Methods

Discover how to use this beautiful SDK. 🖥

CRUD

find(contentType, params)

  • Returns Promise<T>

Get a list of content type entries matching the query filters. You can read here for available parameters.

await strapi.find("restaurants", { name: "La Fourchette" });

findOne(contentType, id)

  • Returns Promise<T>

Get a specific content type entry by id.

await strapi.findOne("restaurants", 1);

count(contentType, params)

  • Returns Promise<T>

Count content type entries matching the query filters. You can read here for available parameters.

await strapi.findOne("restaurants", 1);

create(contentType, data)

  • Returns Promise<T>

Create a content type entry and returns its value.

await strapi.create("restaurants", { name: "" });

update(contentType, id, data)

  • Returns Promise<T>

Update a content type entry by id. It returns the updated entry.

await strapi.update("restaurants", 1, { name: "" });

delete(contentType, id)

  • Returns Promise<T>

Delete a content type entry by id. It returns the deleted entry.

await strapi.delete("restaurants", 1);

graphql(query)

  • Return Promise<T>

Perform GraphQL request throught axios POST request

await strapi.graphql({
  query: `query {
    restaurants {
      id
      name
    }
  }`,
});
restaurants.js
import gql from "graphql-tag";

export function findRestaurants() {
  const query = gql`
    query {
      restaurants {
        id
        name
      }
    }`;
  return query.loc.source.body;
}
import { findRestaurants } from "restaurants.js";

await strapi.graphql({
  query: findRestaurants(),
});

This method is no longer supported in v1.1.0 & newer since it is better to use a true GraphQL client.

Authentication

register(data)

  • Returns Promise<StrapiAuthenticationResponse>

Register a new User & sets the Token.

await strapi.register({ username: "", email: "", password: "" });

See the Strapi documentation.

login(data)

  • Returns Promise<StrapiAuthenticationResponse>

Authenticate a User & sets the Token.

await strapi.login({ identifier: "", password: "" });

See the Strapi documentation.

forgotPassword(data)

  • Returns Promise<void>

Send an email to a user in order to reset his password.

await strapi.forgotPassword({ email: "" });

See the Strapi documentation.

resetPassword(data)

  • Returns Promise<StrapiAuthenticationResponse>

Reset the user password & sets the Token

await strapi.resetPassword({
  code: "",
  password: "",
  passwordConfirmation: "",
});

See the Strapi documentation.

sendEmailConfirmation(data)

  • Returns Promise<void>

Send programmatically an email to a user in order to confirm his account.

await strapi.sendEmailConfirmation({ email: "" });

See the Strapi documentation.

getProviderAuthenticationUrl(provider)

  • Returns string

Get the correct URL of the provider's authentication page to authenticate with it.

strapi.getProviderAuthenticationUrl("provider");

For the list of all providers, see Strapi documentation.

authenticateProvider(provider, access_token)

  • Returns Promise<StrapiAuthenticationResponse>

Once authorized, the provider will redirects the user to your app with an access token in the URL. The access_token parameter is not needed if you have it in your URL redirection but you can provide one.

await strapi.authenticateProvider("provider");
// OR
await strapi.authenticateProvider("provider", "myAccessToken");

For the list of all providers, see Strapi documentation.

logout()

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

strapi.logout();

setUser(user)

Set local data of the logged-in user

strapi.setUser(user);
You can also use strapi.user in order to set user data and get it.

fetchUser()

  • Returns Promise

You often need to fetch your user data. Use this method to fetch the current user from /users/me if a JWT is present in your storage. It sets the User.

await strapi.fetchUser();

setToken(token)

Set token in Axios headers as a Bearer JWT & store it in chosen storage.

strapi.setToken(token);

removeToken()

Remove token from Axios headers & chosen storage.

strapi.removeToken();

Extends

axios

This SDK uses axios under the hood, you can access the axios instance directly from there:

strapi.axios;

OR if you defined custom routes in your Strapi API that go out of the REST scope or if you want to extend the axios request config, you can do as below::

const response = await strapi.request("get", "/restaurants", {
  headers: {
    foo: "bar",
  },
});

See the axios config