3.0.0

Methods

Discover how to use this beautiful SDK. 🖥

CRUD

find

Retrieve a list of content type entries that match the query filters.

Minimal
await strapi.find<Restaurant>('restaurants')
With parameters
await strapi.find<Restaurant>('restaurants', {
  filters: {
    name: {
      $eq: 'La Fourchette'
    }
  },
  sort: 'name:asc',
  pagination: {
    start: 0,
    limit: 0
  },
  fields: [...],
  populate: [...] || '' || {},
  publicationState: 'live',
  locale: 'all'
})

Check out the Strapi documentation for more information.

findOne

Retrieve a specific content type entry by its documentId. You can enhance your query by adding filters to select the returning fields and populate relations.

Minimal
await strapi.findOne<Restaurant>('restaurants', 'hgv1vny5cebq2l3czil1rpb3')
With parameters
await strapi.findOne<Restaurant>('restaurants', 'hgv1vny5cebq2l3czil1rpb3', {
  fields: ['id', 'name']
  populate: ['menu'],
})

create

Create a content type entry and returns its value. You can apply query filters to choose the returning fields and populate relations.

Minimal
await strapi.create<Restaurant>('restaurants', { name: 'The Fork' })
With parameters
await strapi.create<Restaurant>('restaurants', { name: 'The Fork' }, {
  fields: ['id', 'name']
  populate: ['menu'],
})

update

Update a content type entry by its documentId, and receive the updated entry in return. You can apply query filters to specify the returning fields and populate relations.

Minimal
await strapi.update<Restaurant>('restaurants', 1, { name: 'The Fork' })
With parameters
await strapi.update<Restaurant>('restaurants', 1, { name: 'The Fork' }, {
  fields: ['id', 'name']
  populate: ['menu'],
})

delete

  • Parameters
    • contentType: string
    • documentId: string
  • Returns: Promise<void>

Delete a content type entry by its documentId

  await strapi.delete<Restaurant>('restaurants', 'hgv1vny5cebq2l3czil1rpb3')
With Strapi v5, DELETE requests only send a 204 HTTP status on success and no longer return data in the response body.

Authentication

register

Register a new User and set the associated Token.

const { user, jwt } = await strapi.register({ username: '', email: '', password: '' })

Check out the Strapi documentation for more information.

login

Authenticate a User and set the associated Token.

const { user, jwt } = await strapi.login({ identifier: '', password: '' })

Check out the Strapi documentation for more information.

logout

It logs out the user by removing the authentication token from the chosen storage & axios header.

strapi.logout()

changePassword

Change the password for the currently logged-in user.

const { user, jwt } = await strapi.changePassword({ currentPassword: '', password: '', passwordConfirmation: '' })

Check out the Strapi documentation for more information.

forgotPassword

Change the password for the currently logged-in user.

await strapi.forgotPassword({ email: '' })

Check out the Strapi documentation for more information.

resetPassword

Reset the user password and set the associated Token.

const { user, jwt } = await strapi.resetPassword({ code: '', password: '', passwordConfirmation: '' })

Check out the Strapi documentation for more information.

sendEmailConfirmation

Send an email to a user programmatically to confirm their account.

await strapi.sendEmailConfirmation({ email: '' })

Check out the Strapi documentation for more information.

getProviderAuthenticationUrl

Retrieve the correct authentication page URL for a given provider.

window.location = strapi.getAuthenticationProvider('provider')

Check out the Strapi Provider list for more information.

authenticateProvider

After authorization, the provider will redirect the user to your frontend with an access token in the URL. The access_token parameter is not necessary if it's already included in your URL redirection, but you can provide one if needed.

Token in URL
const { user, jwt } = await strapi.authenticateProvider('provider')
Provide a token
const { user, jwt } = await strapi.authenticateProvider('provider', 'my_access_token')

Check out the Strapi documentation for more information.

fetchUser

Fetching user data is a common requirement. You can use this method to retrieve the current user from /users/me when a JWT is stored in your storage. It then sets the User.

const user = await strapi.fetchUser()

getToken

  • Returns: string | null

Retrieve your JWT token from selected storage.

const token = strapi.getToken()

setToken

  • Parameters
    • token: string
  • Returns: void

Set your JWT token in axios headers as a Bearer token and store it in the selected storage.

const token = strapi.setToken('my_jwt_token')

removeToken

  • Returns: void

Remove your JWT token from axios headers and the selected storage.

strapi.removeToken()