Strapi JavaScript SDK

0.3.3 · abandoned · verified Sun Apr 19

The `strapi-sdk-javascript` package provides a client library for interacting with a Strapi backend, offering methods for CRUD operations, local and provider-based authentication (Facebook, GitHub, Google, Twitter), and file management. It aims to simplify API calls by abstracting HTTP requests. However, the package is officially unmaintained and deprecated by the Strapi team, who recommend using a standard HTTP client (like Axios or Fetch) configured for your Strapi API endpoints instead. The current stable version is 0.3.3, but it has not seen active development or releases for a significant period. Its key differentiator as an 'official' SDK is now moot due to its abandonment, with the recommended approach being direct API interaction. Developers should be aware it does not support newer Strapi features or GraphQL roadmap items.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the Strapi SDK, perform local user authentication, and fetch entries from a Strapi API, including error handling. It uses environment variables for configuration.

import Strapi from 'strapi-sdk-javascript';

const STRAPI_BASE_URL = process.env.STRAPI_URL ?? 'http://localhost:1337';
const STRAPI_USERNAME = process.env.STRAPI_USERNAME ?? 'testuser';
const STRAPI_PASSWORD = process.env.STRAPI_PASSWORD ?? 'password';

async function runStrapiSdkExample() {
  console.log(`Connecting to Strapi at: ${STRAPI_BASE_URL}`);
  const strapi = new Strapi(STRAPI_BASE_URL);

  try {
    // Local authentication
    console.log('Attempting local login...');
    await strapi.login(STRAPI_USERNAME, STRAPI_PASSWORD);
    console.log('Login successful! Token stored.');

    // Fetch entries (e.g., 'posts')
    const posts = await strapi.getEntries('posts');
    console.log(`Fetched ${posts.length} posts:`, posts.map(p => p.title));

    // Example: Create an entry (assuming 'articles' is a content type)
    // const newArticle = await strapi.createEntry('articles', { title: 'SDK Test Article', content: 'Content from SDK' });
    // console.log('Created new article:', newArticle);

    // Example: Get entry count
    const postCount = await strapi.getEntryCount('posts');
    console.log(`Total posts count: ${postCount}`);

  } catch (error) {
    console.error('Error during Strapi SDK operations:', error.message);
    if (error.response) {
      console.error('Server response:', error.response.data);
    }
  }
}

runStrapiSdkExample();

view raw JSON →