Strapi Client
The Strapi Client library simplifies interactions with your Strapi back end, providing a way to fetch, create, update, and delete content. This guide walks you through setting up the Strapi Client, configuring authentication, and using its key features effectively.
Getting Started
- A Strapi project has been created and is running. If you haven't set one up yet, follow the Quick Start Guide to create one.
- You know the URL of the Content API of your Strapi instance (e.g.,
http://localhost:1337/api).
Installation
To use the Strapi Client in your project, install it as a dependency using your preferred package manager:
- Yarn
- NPM
- pnpm
yarn add @strapi/client
npm install @strapi/client
pnpm add @strapi/client
Basic configuration
To start interacting with your Strapi back end, initialize the Strapi Client and set the base API URL:
- JavaScript
- TypeScript
- Browser (UMD)
With Javascript, import the strapi function and create a client instance:
import { strapi } from '@strapi/client';
const client = strapi({ baseURL: 'http://localhost:1337/api' });
With Typescript, import the strapi function and create a client instance with your Strapi API base URL:
import { strapi } from '@strapi/client';
const client = strapi({ baseURL: 'http://localhost:1337/api' });
If you're using the Strapi Client in a browser environment, you can include it using a <script> tag.
<script src="https://cdn.jsdelivr.net/npm/@strapi/client"></script>
<script>
const client = strapi.strapi({ baseURL: 'http://localhost:1337/api' });
</script>
The baseURL must include the protocol (http or https). An invalid URL will throw an error StrapiInitializationError.