Backend API
Swell for Node.js (swell-node) is an API-wrapper written in Javascript aimed at backend development. It provides some convenient methods for performing CRUD operations on your Swell store data using a server-side JavaScript application. It also allows you to connect multiple store instances within the same process.
In this guide, we’ll review what frameworks can be used with swell-node, some commonly used Node.js methods, and the options available when initializing the client.
Find the swell-node library on Github.
npm install swell-node --saveThe library can be used with frameworks that support server-side rendering like Next.js, Astro, and others. This library can also be used with a standalone server-side application written in Node.js.
swell-node is used for querying and modifying backend data such as account, product, and cart details. Some of the most commonly used methods in the library include:
- init
- createClient
- get, put, post, delete
In this section, we’ll go over what each method is used for, the parameters for each one, and see an example of how it’s used.
The init method is used to initialize the client. The parameters for this method are clientId, clientKey, and options.
The optional options parameter accepts url, verifyCert, version, timeout, headers, retries, maxSockets, and keepAliveMs. Use timeout to set a request timeout in milliseconds, and retries to retry requests that fail with a connection error (default 0).
const { swell } = require('swell-node');
swell.init('my-store', 'secret-key');The createClient method returns a new client instance, allowing you to connect to multiple stores within the same process. The parameters for this method are clientId, clientKey, and optionally options.
const client1 = swell.createClient('my-store-1', 'secret-key-1');
const client2 = swell.createClient('my-store-2', 'secret-key-2');The get/put/post/delete methods are CRUD operations used to manage and edit your Swell store data and content models. You can also use these CRUD operations to implement custom logic to your store’s data. The parameters for these methods are url and data. They return a promise with the result.
// Retrieve all active products.
const products = await swell.get('/products', { active: true })