> For the complete documentation index, see [llms.txt](https://developers.swish.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.swish.app/libraries/node.js.md).

# Node.js

JS library to integrate Swish on a Node.js server.

## Swish app proxy for Node.js

The Swish browser library requires a proxy service that operates on the same domain as the store. This service facilitates the loading of additional resources and ensures secure communication with the Swish API.

### Installation

```sh
npm i @swishapp/node
```

### Usage

The example below is for Remix and needs to be adjusted for other frameworks.

```js
import { ActionFunction, LoaderFunction } from "@remix-run/node";
import { createProxy, MemoryStorage } from "@swishapp/node";

const swishProxy = createProxy({
  basePath: "/swish", // Needs to match the route path
  authToken: process.env.SWISH_API_TOKEN,
  storage: new MemoryStorage(), // Use MemoryStorage for development only!
  authenticate: async (request) => {
    // Authenticate the request and return customer id or null
    // Throw an error if the request cannot be authenticted
    return null;
  },
  onError: (error) => {
    console.error(error);
  },
});

export const loader: LoaderFunction = async ({ request }) =>
  swishProxy.forward(request);

export const action: ActionFunction = async ({ request }) =>
  swishProxy.forward(request);
```

The Swish app for browsers won't work on the server. Use it as a client only script!

```ts
// The proxy also serves the Swish brwoser app
import { createApp } from "/swish/assets/swish.js"; // Assuming the proxy runs on /swish

const swish = await createApp({
  proxy: {
    baseUrl: "/swish", // Use proxy route path
  },
});
```
