Authenticated Requests to Strapi

by Sujay Prabhu, Senior System Analyst

This blog covers how to make requests to Strapi endpoints as an authenticated user in Strapi v3 & v4.

Before starting, I have created Employees collection type and added some employees.

With Strapi v3

  • Lets try to fetch employee details by making a GET request

JavaScript

// Request

curl --request GET 'http://localhost:1337/employees'

// Response

{
    "statusCode": 403,
    "error": "Forbidden",
    "message": "Forbidden"
}
  • As collections are restricted by default, it cannot be accessed as a Public user resulting in 403 status code.
  • To get rid of the Forbidden error, lets add permission to /employees endpoint by enabling:
Settings -> Users & Permissions plugin -> Roles -> Authenticated -> Employees -> find
  • A JWT token should be added to API request to fetch data from restricted endpoints
  • To get the JWT token, create a user and get the user authenticated.

JavaScript

// Request

curl --request POST 'localhost:1337/auth/local' \
--form 'identifier="[email protected]"' \
--form 'password="test@123"'

// Response

{
    "jwt": TOKEN,
    "user": {
        "id": 3,
        "username": "test",
        "email": "[email protected]",
        "provider": "local",
        "confirmed": false,
        "blocked": false,
        "role": {
            "id": 1,
            "name": "Authenticated",
            "description": "Default role given to authenticated user.",
            "type": "authenticated"
        },
        "created_at": "2022-04-21T14:01:32.672Z",
        "updated_at": "2022-04-21T14:01:32.679Z"
    }
}

  • Now, add the JWT token obtained in last step to our first step request's Authorization header

JavaScript

// Request

curl --request GET 'localhost:1337/employees' \
--header 'Authorization: Bearer TOKEN'

// Response

[
    {
        "id": 1,
        "name": "test",
        "age": null,
        "published_at": "2022-04-13T06:27:46.430Z",
        "created_at": "2022-04-13T06:27:44.423Z",
        "updated_at": "2022-04-13T06:27:46.441Z"
    }
]

With Strapi v4

In Strapi v4, they have added another way to get the restricted content.

  • First way is same as the v3's, based on roles and Permissions.
  • This approach can be followed when the requirement is to restrict contents based on roles.
  • Learn more about authenticated requests to Strapi by assigning permissions to roles here
Note: In Strapi v4, endpoint is changed to `localhost:1337/api/employees`
  • Second way is by making use of API tokens, which is a built-in feature in Strapi v4.
  • This allows executing request on restricted endpoints as an authenticated user without the hassle of roles and permissions.
  • To generate API tokens, click on
Settings -> API tokens -> Create new token
  • Copy the token generated and add it to request's Authorization header

JavaScript

// Request

curl --request GET 'localhost:1337/api/employees' \
--header 'Authorization: Bearer API_TOKEN'

// Response

{
    "data": [
        {
            "id": 1,
            "attributes": {
                "name": "test",
                "age": 50,
                "createdAt": "2022-04-18T15:46:02.760Z",
                "updatedAt": "2022-04-18T15:46:06.891Z",
                "publishedAt": "2022-04-18T15:46:06.887Z"
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}
  • Learn more about making authenticated requests to Strapi using API tokens here

More articles

Agentic Memory: A Practical Guide to How AI Agents Remember

A practical guide to agentic memory, covering working, semantic, episodic, and procedural memory, along with storage, retrieval, distillation, and memory evolution.

Read more

What is `llms.txt`, Why It Matters, and How to Use It

What llms.txt is, how it fits into Agentic Engine Optimization (AEO), how it differs from robots.txt and sitemap.xml, and how to add one to your site.

Read more

Your competitors are already using AI.
The question is how fast you want to unlock the value.

Don't know where to start?

AI is everywhere but it's unclear which investments will actually move your metrics and which are expensive experiments.

Your data isn't ready

Most AI projects fail at the data layer. Pipelines, quality, access all need work before LLMs can deliver value.

Internal teams are stretched

Your engineers are shipping product. They don't have capacity to also become AI specialists with production-grade experience.

Legacy systems block everything

Aging, undocumented codebases make AI integration slow, risky, and expensive. They need to move first.

Don't worry. We've got you covered.

Start with the audit.