2.3.3

Methods

Discover how to use this beautiful SDK. 🖥

CRUD

find

Retrieve a list of content type entries that match the query filters. Strapi v4 introduces new 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 ID. You can enhance your query by adding filters to select the returning fields and populate relations.

Minimal
await strapi.findOne<Restaurant>('restaurants', 1)
With parameters
await strapi.findOne<Restaurant>('restaurants', 1, {
  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 ID, 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

Delete a content type entry by its ID, and receive the deleted entry in return. You can apply query filters to define the returning fields and populate relations.

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

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

v2.3.0+Strapi v4.3.3+

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.

setUser

Set local data for the logged-in user.

strapi.setUser(user)
This method is no longer supported in v2.2.0+ since the getter and setter for user properties have been removed. Now, you can use strapi.user to both set and get user data.

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

v2.2.0+
  • Returns: string | null

Retrieve your JWT token from selected storage.

const token = strapi.getToken()

setToken

v2.2.0+
  • 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

v2.2.0+
  • Returns: void

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

strapi.removeToken()