Usage
New instance
Here is how you can instantiate the Strapi SDK. Note that the displayed options are the default ones. 😉
import Strapi from "strapi-sdk-js";
const strapi = new Strapi();
// OR with options
const strapi = new Strapi({
url: process.env.STRAPI_URL || "http://localhost:1337",
store: {
key: "strapi_jwt",
useLocalStorage: false,
cookieOptions: { path: "/" },
},
axiosOptions: {},
});
See options for available options.
Content Types
All contentTypes methods are built around the default Strapi CRUD operations.
find
findOne
count
create
update
delete
await strapi.find("restaurants", { ...params });
See more in Methods
GraphQL
This method is no longer supported in v1.1.0 & newer since it is better to use a true GraphQL client.
await strapi.graphql({
query: `query Restaurants {
restaurants {
id
name
}
}`,
});
See more in Methods#graphql
Authentication
Here are the methods in order to handle authentication in your application:
Register
const { user, jwt } = await strapi.register({
email: "",
username: "",
password: "",
});
Login
const { user, jwt } = await strapi.login({ identifier: "", password: "" });
Logout
strapi.logout();
Forgot Password
await strapi.forgotPassword({ email: "" });
Reset Password
const { user, jwt } = await strapi.resetPassword({
code: "",
password: "",
passwordConfirmation: "",
});
Email Confirmation
await strapi.sendEmailConfirmation({ email: "" });
Get Auth Provider URL
window.location = strapi.getAuthenticationProvider("provider");
See providers list
Authenticate Provider
await strapi.authenticateProvider("provider", "access_token");
// OR with params query
await strapi.authenticateProvider("provider");
See providers list
User
Once you're logged in, you can access the user
object which contains details about authenticated user:
strapi.user;
Advanced
Accessing 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