How To Create API End Points Using STRAPI

jhabar singh Bhati
4 min readNov 5, 2020
Image from Strapi’s official page

Hi Friends !!

In this article I am going to a create Simple API end point using Strapi and I assure you that once you read this article you would start loving Strapi like me.

Let’s see what are we going to build

simple schema we will be implementing using strapi

Now we will implement above schema in Strapi. API end points corresponding to those collections in the schema will be created for us by Strapi . Isn’t that great.

Let’s Begin

open you terminal and follow the below steps:

yarn create strapi-app my-project --quickstart # type this command

Note : yarn should be installed on you desktop.

If yarn is not installed then go to the above link, install it and then run the above CLI command.

Post Installation

Once your installation gets over your server will start automatically and you will see a login form as shown below or you can run the server using the below command.

npm run devlop
Enter your details to login to the Strapi content management page

Once you are logged in you will be redirected to the content management page.

Strapi home screen

Now go to content type builder

click on this button to create collections.

Follow these steps to create the collections.

Follow this video to create collections

Once you have created the collection you need to install postman to test the API End points.

Download Postman

Click on the link given above to download the Postman, once you have installed the postman follow the steps shown below

  1. Changing Role setting

Go to settings > USERS & PERMISSIONS PLUGIN > roles > public

select these check boxes and save the changes

select the check boxes as shown above and save the changes

Go to settings > USERS & PERMISSIONS PLUGIN > roles > authenticated

Select the check boxes as shown above and save the changes

Basically what we are doing is that only authenticated users would be able to create, delete or update the data but data could be retrieved by anyone.

2. Create a user

Go to COLLECTION TYPES > Users

add the user

Add a user who will be able to create, delete or update the data

NOTE : Remember set the role as Authenticated

3. Now open the POSTMAN and play with the API’s

Make a POST request to “http://localhost:1337/auth/local

{
"identifier": "test@gmail.com",
"password": "test@123"
}

In body you should send this data

NOTE: Use the credentials you had created in step 2

you will receive a JWT token after making post request

Now using this JWT token we will perform basic CRUD operations in Stapi

4. Adding the JWT token

Adding jwt token

As shown in the above image go to Type > Bearer token and add that JWT token in the Token.

JWT token added

Now every time you will make a request token will be sent with that request to the server and it will be verified using the secret key(signature).

CRUD Operations with STRAPI

Let’s create a user

  1. Make POST request to “http://localhost:1337/my-users” and send the below data as well in the body.
{
"username" : "test1"
"email" : "jhab@gmail.com"
"password" : "test@1231"
"mobile" : "95648745123"
}

Note : you can create as many users as you want and if you had not added JWT token then it won’t let you add it in the database.

2. Make GET request to “http://localhost:1337/my-users

[
{
"id" : 1,
"username" : "test1"
"email" : "jhab@gmail.com"
"password" : "test@1231"
"mobile" : "95648745123"
},
....
]

you will get an array of all the users detail

3. Make DELETE request to “http://localhost:1337/my-users/1

It will delete the user with id 1

Similarly you can create the comments and post

ENDPOINTS

  1. Comments “http://localhost:1337/comments/
  2. Post “http://localhost:1337/posts/”

NOTE: In post and comments there are Foreign keys or Relation fields so you need to store the Primary keys of the field with whom it is related. So in Strapi by default Id field is there which is a Primary key, So you should store the Id of the column with which it is related.

You can create even more complex API’s using Strapi and integrate it with the Frontend frameworks like React, Vue or Angular.

--

--