Backend API
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 methods, and how to edit this library’s caching behavior.
See our backend API docs for code examples using Node.
For Swell-node, check out this repo.
npm install --save swell-node
Swell-node can be used with frameworks that support server-side rendering like Next.js. This library can also be used with a standalone server-side application written in Swell-node.
Swell-node is frequently used for querying and retrieving backend data like account, product, and cart details. Some of the most commonly used methods in the library include:
- init
- createClient
- get/put/post/delete
- close
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 Node 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 })
The close method is used to close the server connection manually. Parameters for this method are not necessary.
swell.close()
Swell-node provides some in-memory caching enabled by default. For records that don't change frequently, such as products, it will always return from cache when possible.
To disable caching behavior, use the option cache: false.
swell.init('my-store', 'secret-key', {
cache: false,
});
Now that we've reviewed Swell-node, let’s dive into another backend API library, Swell-php.