# Valise API

> Build developer integrations on top of Valise.

> The API is a technical feature meant for developers. If you’re looking for
> ways to get your data out of Valise, check out our [export
> options](https://valise.works/help/exports).

Valise offers a REST API for programmatically accessing your data. You can use this API to build integrations on top of Valise, such as:

- Powering a website with artworks from your vault
- Updating data in other systems when it changes in Valise

## Getting started

To start using the API, [create an API key](https://valise.works/a/me/settings/developer) from the [developer settings](https://valise.works/a/me/settings/developer) of your dashboard. You can use this key to authenticate to the API calls described in the [endpoints](https://valise.works/help/api#endpoints) section.

## Overview

### Base URL

All requests should be made to the following base URL:

```http
https://api.valise.works
```

### Authentication

The API uses API keys to authenticate requests. For any endpoint, pass a `Bearer` token in the `Authorization` header of the request.

```http
Authorization: Bearer <API_KEY>
```

```shell
curl https://api.valise.works/v0/artworks \
  --header "Authorization: Bearer <API_KEY>"
```

```js
fetch("https://api.valise.works/v0/artworks", {
  headers: {
    Authorization: "Bearer <API_KEY>",
  },
})
```

To generate an API key, go to the [developer settings](https://valise.works/a/me/settings/developer) of your dashboard and press the **Create API key** button. We email the vault's owners each time someone creates a key, with the key's name, its scope, and a link to revoke it.

API keys are associated with a vault, not with a user account, so you don't need to worry about leaving a vault revoking the key’s access. By default, keys don’t expire.

### Permissions

When you create a key, you choose its scope:

- **Read-only** keys can fetch data using the `GET` endpoints. This is the default.
- **Read and write** keys can additionally create, update, and delete records.

If a read-only key is used on a write endpoint, the API responds with a `403` error. Keys created before scopes were introduced are read-only.

### Pagination

When an API response returns a list of records, information about pages will be returned in the response.

By default, up to 100 items are returned per page, which is the maximum value. You can change this limit by passing the `limit` query parameter to the endpoint.

Paginated responses will look like this:

```json
{
  "data": [ ... ],
  "page": {
    "count": 100, // Number of items in the current page
    "next": "https://api.valise.works/..." // Next page of results, or null if no more
  }
}
```

You can use the `page.next` URL to make additional requests to get all the records by looping through the values until they return `null`.

For example, here's how you could fetch all the artworks in your vault in JavaScript:

```js
const fetchArtworks = async () => {
  let artworks = []
  let next = "https://api.valise.works/v0/artworks"
  while (next) {
    // Note: authentication and error handling are omitted here for brevity.
    const response = await fetch(next)
    const data = await response.json()
    artworks.push(...data.data)
    next = data.page.next
  }
  return artworks
}
```

### Rate limiting

The API allows up to **300 requests per minute**. Every response includes headers so you can track your usage:

| Header                  | Description                                          |
| ----------------------- | ---------------------------------------------------- |
| `X-RateLimit-Limit`     | Maximum requests allowed per window (300)            |
| `X-RateLimit-Remaining` | Requests remaining in the current window             |
| `Retry-After`           | Seconds to wait before retrying (only on 429 errors) |

If you exceed the limit, the API will return a `429` status code. You should wait for the number of seconds in the `Retry-After` header before retrying.

### Response codes

We use standard HTTP codes to indicate the success or failure of a request. In general, `2xx` codes indicate success, `4xx` codes indicate client errors, and `5xx` codes indicate infrastructure issues.

| Status | Description                                        |
| ------ | -------------------------------------------------- |
| `200`  | Successful request                                 |
| `201`  | A resource was created                             |
| `204`  | The request succeeded with no response body        |
| `400`  | Check that the request parameters are correct      |
| `401`  | No API key was provided with the request           |
| `403`  | The API key is invalid or not permitted to do this |
| `404`  | The resource wasn't found                          |
| `405`  | The endpoint doesn't support this HTTP method      |
| `409`  | The request conflicts with existing data           |
| `411`  | A file was sent without a `Content-Length` header  |
| `413`  | The request body or file is too large              |
| `429`  | The rate limit has been exceeded                   |
| `500`  | An error happened with Valise’s servers            |

### Errors

When an API request fails, Valise returns a structured error response that follows this format:

```json
{
  "error": {
    "code": "unauthenticated",
    "message": "Please provide a valid API key with your request."
  }
}
```

The HTTP status code should give you a general sense of the error category, and the `error.code` field will give you a more specific reason for the error.

When a request fails validation, the response also includes a `details` array describing every problem at once. Each entry names the offending `field` (when applicable) and a `message`:

```json
{
  "error": {
    "code": "bad_request",
    "message": "The request is invalid.",
    "details": [
      { "field": "title", "message": "Must be at most 500 characters." },
      { "field": "uid", "message": "Must be a valid UID, e.g. \"VD-123\"." }
    ]
  }
}
```

Field names in a request body are case-sensitive. A body with an unknown field, or with the same field twice, will return a `400` error.

## Endpoints

The API currently covers artworks, collections, tags, locations, contacts, and file uploads. Each has its own page:

- [Artworks](https://valise.works/help/api/artworks): List, create, update, and delete artworks.
- [Collections](https://valise.works/help/api/collections): Manage collections and the artworks in them.
- [Tags](https://valise.works/help/api/tags): Manage tags and attach them to artworks.
- [Locations](https://valise.works/help/api/locations): Manage the places where works are kept.
- [Contacts](https://valise.works/help/api/contacts): Manage the people and organizations in your vault.
- [Uploads](https://valise.works/help/api/uploads): Send files to Valise and link them to records.

We're developing endpoints for other record types like sales, consignments, presentations, and press. If there's an endpoint you need, [please let us know](mailto:team@valise.works), we're happy to prioritize building it.

## Community resources

Valise users have built framework specific integrations on top of our API that may be helpful when working with the API:

- [astro-loader-valise](https://github.com/zakj/astro-loader-valise), for loading collections and artworks into an [Astro](https://astro.build/)-based site.

## Related articles

- [Artworks API](https://valise.works/help/api/artworks): List, create, update, and delete artworks.
- [Exporting data](https://valise.works/help/exports): Back up your data or take it with you
