DEV Community

Shannon W
Shannon W

Posted on • Updated on

Setting Up a REST API With Strapi

Original post from series "Building an App with Gatsby and Strapi"

Who is this post for? Each post in the series is loosely dependent on each other. So you can follow any post in any order you like. Posts were made this way so you don’t have to read all the previous ones to follow along.

The goal of this post is to explore Strapi’s REST API in regards to:

  • setting up the API,
  • setting up API permissions,
  • documenting and testing API endpoints.

Setting Up

Okay, so far the post is a bit misleading… Strapi automatically generates content API endpoints for CRUD (create, read, update, delete) operations when you create or modify content types 🙂 So we don’t have to worry about this step!

Setting Up API Permissions

Before we can use the generated API, we will need to define who has permission and to what actions. For example, lets look at the Page content type and give the Public role permission to call count, find, and findone actions (actions are simply the functions used by the controller to perform business logic).

Step 1 – Ensure the content type is defined as needed
Alt Text

Step 2 – Go to Roles & Permissions and select the Public role
Alt Text

Step 3 – Give permission to use the actions
Alt Text

Documenting and Testing the API

There are multiple ways to test this, but for this post I recommend is using Swagger UI. It’s a nifty tool that automatically generates documentations for REST APIs. To set up, run the following command in your terminal in the root folder of the strapi instance.

   npm run strapi install documentation
Enter fullscreen mode Exit fullscreen mode

Restart Strapi. A new UI section is then created to manage the documentation. Here you can access the documentation, test responses, and restrict access to the documentation.

Alt Text

Top comments (0)