Connect your JavaScript/TypeScript apps to a flexible and fully customizable Strapi backend with ease.
Before you begin, ensure you have the following:
- A Strapi backend up and running: quick start guide.
- The API URL of your Strapi instance: for example,
http://localhost:1337/api
. - A recent version of Node.js installed.
Install the SDK as a dependency in your project:
NPM
npm install @strapi/sdk-js
Yarn
yarn add @strapi/sdk-js
pnpm
pnpm add @strapi/sdk-js
To interact with your Strapi backend, initialize the SDK with your Strapi API base URL:
import { strapiSDK } from '@strapi/sdk-js';
const sdk = strapiSDK({ baseURL: 'http://localhost:1337/api' });
Alternatively, use a <script>
tag in a browser environment:
<script src="https://cdn.jsdelivr.net/npm/@strapi/sdk-js"></script>
<script>
const sdk = strapi.strapiSDK({ baseURL: 'http://localhost:1337/api' });
</script>
The SDK supports multiple authentication strategies for accessing authenticated content in your Strapi backend.
If your Strapi instance uses API tokens, configure the SDK like this:
const sdk = strapiSDK({
baseURL: 'http://localhost:1337/api',
auth: {
strategy: 'api-token',
options: { token: 'your-api-token-here' },
},
});
The Strapi SDK instance provides key properties and utility methods for content and API interaction:
baseURL
: base URL of your Strapi backend.fetch
: perform generic requests to the Strapi Content API using fetch-like syntax..collection(resource: string)
: get a manager instance for handling collection-type resources..single(resource: string)
: get a manager instance for handling single-type resources.
The .collection()
method provides a manager for working with collection-type resources,
which can have multiple entries.
Note: the resource
corresponds to the plural name of your collection type, as defined in the Strapi model.
find(queryParams?)
: fetch multiple entries.findOne(documentID, queryParams?)
: fetch a single entry by its ID.create(data, queryParams?)
: create a new entry.update(documentID, data, queryParams?)
: update an existing entry.delete(documentID, queryParams?)
: remove an entry.
const articles = sdk.collection('articles');
// Fetch all english articles sorted by title
const allArticles = await articles.find({
locale: 'en',
sort: 'title',
});
// Fetch a single article
const singleArticle = await articles.findOne('article-document-id');
// Create a new article
const newArticle = await articles.create({ title: 'New Article', content: '...' });
// Update an existing article
const updatedArticle = await articles.update('article-document-id', { title: 'Updated Title' });
// Delete an article
await articles.delete('article-id');
The .single()
method provides a manager for working with single-type resources, which have only one entry.
Note: the resource
corresponds to the singular name of your collection type, as defined in the Strapi model.
find(queryParams?)
: fetch the document.update(data, queryParams?)
: update the document.delete(queryParams?)
: remove the document.
const homepage = sdk.single('homepage');
// Fetch the default version of the homepage
const defaultHomepage = await homepage.find();
// Fetch the spanish version of the homepage
const spanishHomepage = await homepage.find({ locale: 'es' });
// Update the homepage draft content
const updatedHomepage = await homepage.update(
{ title: 'Updated Homepage Title' },
{ status: 'draft' }
);
// Delete the homepage content
await homepage.delete();
This section provides guidance on enabling and managing debug logs for the SDK, powered by debug.
In Node.js bundles (cjs
, esm
), debugging capabilities are always available to use.
You can turn on or off debug logs using the DEBUG
environment variable:
# Enable logs for all namespaces
DEBUG=*
# Enable logs for a specific namespace
DEBUG=sdk:http
# Turn off logs
unset DEBUG
For browser environments, debug capabilities are intentionally turned off to optimize the bundle size.
The debug
tool allows you to control logs using wildcard patterns (*
):
*
: enable all logs.sdk:module
: enable logs for a specific module.sdk:module1,sdk:module2
: enable logs for multiple modules.sdk:*
: match all namespaces undersdk
.sdk:*,-sdk:module2
: enable all logs except those fromsdk:module2
.
Below is a list of available namespaces to use:
Namespace | Description |
---|---|
sdk:core |
Logs SDK initialization, configuration validation, and HTTP client setup. |
sdk:validators:sdk |
Logs details related to SDK configuration validation. |
sdk:validators:url |
Logs URL validation processes. |
sdk:http |
Logs HTTP client setup, request processing, and response/error handling. |
sdk:auth:factory |
Logs the registration and creation of authentication providers. |
sdk:auth:manager |
Logs authentication lifecycle management. |
sdk:auth:provider:api-token |
Logs operations related to API token authentication. |
sdk:auth:provider:users-permissions |
Logs operations related to user and permissions-based authentication. |
sdk:ct:collection |
Logs interactions with collection-type content managers. |
sdk:ct:single |
Logs interactions with single-type content managers. |
sdk:utils:url-helper |
Logs URL helper utility operations (e.g., appending query parameters or formatting URLs). |