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 how to edit this library’s caching behavior.
Find the swell-node library on Github.
npm install swell-node --save
The 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.
Default host, port, routing, and session can be passed as options.
swell.init('client-id', 'client-key')
The createClient method is used to connect to multiple stores within the same request. The parameters for this method are clientId and clientKey.
swell.createClient('my-store-1', 'secret-key-1')
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 this method are url, data, and callback.
// Retrieve all active products.
const products = await swell.get(’/products’, { active: true })