# API Client

The Swish API Client provides type-save methods for developers to interact with the API.&#x20;

{% hint style="warning" %}
When integrating Swish into a browser environment, using the [Browser library](https://developers.swish.app/libraries/browser) is recommended.
{% endhint %}

## Requirements

This packages works in all JS environments were the [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) is available.

## Installation

```sh
npm install @swishapp/api-client
```

## Usage

```typescript
import { createApiClient } from "@swishapp/api-client";

const swish = createApiClient({
  authToken: "your-api-auth-token"
});

const { data } = await swish.items.list();
```

## Reference

### Items

{% hint style="success" %}
For more details, refer to the [Items API reference](https://developers.swish.app/swish-api/api-reference/items).
{% endhint %}

#### List all items

```typescript
const { data, error } = await swish.items.list();
```

#### List all items (pagination)

```typescript
const { data, error } = await swish.items.list({
  page: "page-cursor",
  limit: 20,
});
```

#### List all items (search query)

```typescript
const { data, error } = await swish.items.list({
  query: "product:123",
});
```

#### Create new item

```typescript
const { data, error } = await swish.items.create({ 
  productId: 123, variantId: 456, quantity: 1,
});
```

#### Delete multiple items

<pre class="language-typescript"><code class="lang-typescript"><strong>const { error } = await swish.items.delete([
</strong>  "item-id-1",
  "item-id-2",
]);
</code></pre>

#### Find item by ID

```typescript
const { data, error } = await swish.items.findById("item-id");
```

#### Update item by ID

```typescript
const { data, error } = await swish.items.updateById("item-id", {
  variantId: 456,
});
```

#### Delete item by ID

```typescript
const { error } = await swish.items.deleteById("item-id");
```

### Lists

{% hint style="success" %}
For more details, refer to the [Lists API reference](https://developers.swish.app/swish-api/api-reference/lists).
{% endhint %}

#### List all lists

```typescript
const { data, error } = await swish.lists.list();
```

#### Create new list

```typescript
const { data, error } = await swish.lists.create({
  name: "Favourites",
});
```

#### Find list by ID

```typescript
const { data, error } = await swish.lists.findById("list-id");
```

#### Find list by ID (items sorted in custom order)

```typescript
const { data, error } = await swish.lists.findById("list-id", {
  sort: "customer"
});
```

#### Update list by ID

```typescript
const { data, error } = await swish.lists.updateById("list-id", {
  name: "New list name",
});
```

#### Delete list by ID

```typescript
const { error } = await swish.lists.deleteById("list-id");
```

#### Set  custom item order

```typescript
const { data, error } = await swish.lists.orderItems("list-id", {
  itemIds: ["item-id-2", "item-id-1"],
});
```

### Profiles

{% hint style="success" %}
For more details, refer to the [Profiles API reference](https://developers.swish.app/swish-api/api-reference/profiles).
{% endhint %}

#### Create new token

```typescript
const { data, error } = await swish.profiles.createToken({
  customer: "gid://shopify/Customer/1234567",
})
```
