suissard-strapi-client
v1.1.1
Published
A simple JavaScript library for interacting with the Strapi API.
Readme
Strapi API JS
A simple JavaScript library for interacting with the Strapi API.
Installation
npm install suissard-strapi-api-jsUsage
Instantiation
First, you need to instantiate the StrapiApi client with your Strapi base URL, the collections you want to access, and your API token.
import StrapiApi from 'strapi-api-js';
const baseURL = 'http://localhost:1337';
const collections = ['posts', 'authors'];
const token = 'your-api-token';
const strapi = new StrapiApi(baseURL, collections, token);CRUD Operations
You can perform CRUD (Create, Read, Update, Delete) operations on your collections.
List entries
const posts = await strapi.collections.posts.list();Get a specific entry
const post = await strapi.collections.posts.get(1);Create an entry
const newPost = await strapi.collections.posts.create({
title: 'My new post',
content: 'This is the content of my new post.',
});Update an entry
const updatedPost = await strapi.collections.posts.update(1, {
title: 'My updated post',
});Delete an entry
await strapi.collections.posts.delete(1);Authentication
The library also provides methods for user authentication.
Register a new user
const newUser = await strapi.register('username', '[email protected]', 'password');Log in
const user = await strapi.login('[email protected]', 'password');
// The API token will be automatically set for subsequent requests.