# Node.js

Source: https://developers.swell.is/backend-api/backend-libraries/node

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.

> **Tip:** Find the `swell-node` library on Github.

### Installation

**Install swell-node**

`npm`

```
npm install swell-node --save
```

`yarn`

```txt
yarn add swell-node
```

### Using different frameworks

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.

### Methods

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.

#### init

The `init` method is used to initialize the client. The parameters for this method are `clientId`, `clientKey`, and `options`.

> **Note:** 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`).

**Example of the init method**

**Node**

```javascript
const { swell } = require('swell-node');

swell.init('my-store', 'secret-key');
```

#### createClient

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`.

**Example of the createClient method**

**Node**

```javascript
const client1 = swell.createClient('my-store-1', 'secret-key-1');
const client2 = swell.createClient('my-store-2', 'secret-key-2');
```

#### get/put/post/delete

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.

**All methods**

**Node** `GET`

```javascript
// Retrieve all active products.
const products = await swell.get('/products', { active: true })
```

**Node** `POST`

```javascript
// Create a new product.
await swell.post('/products', {
  name: 'Longsword',
  price: 50.00,
  active: true,
  options: [
    {
      name: 'Material',
      values: [
        {
          name: 'Iron',
        },
        {
          name: 'Steel',
        },
				{
          name: 'Silver',
        },
      ],
    },
  ],
});
```

**Node** `PUT`

```javascript
// Update an existing product.
await swell.put('/products/{id}', {
  id: '5c8fb5e1ed2faf8c79da492a',
  price: 29.99,
  // use $set to override values
  $set: {
    options: [
      {
      name: 'Material',
      values: [
        {
          name: 'Ebony',
        },
        {
          name: 'Glass',
        },
      ],
    },
    ],
  },
});
```

**Node** `DELETE`

```javascript
// Delete an existing product.
await swell.delete('/products/{id}', {
  id: '5c8fb5e1ed2faf8c79da492a',
});
```
