# REST API integration

> Authenticate REST requests, discover your catalog, create subscription checkout flows, track usage and handle Komashi API responses.

Canonical: https://komashi.com/docs/provider/Developers/api-integration/
Language: en

Use the REST API to connect your backend to Komashi. The API supports catalog discovery, customer and contact management, subscriptions, checkout links, usage and invoice lookups.

## Find the endpoint and reference

The full list of REST API routes is available in the interactive OpenAPI reference: **[komashi.com/api-docs](https://komashi.com/api-docs/)**.

Open **Control Center → Integration → API**. The page displays **Documentation** and **API endpoint**. Copy the endpoint shown there; it already includes the version prefix.

[![API Keys page showing the documentation link and API endpoint](https://komashi.com/docs/assets/images/integrations-api-keys.png)](/assets/images/integrations-api-keys.png)

For the public service, the versioned endpoint is:

```text
https://api.komashi.com/v1
```

A local development environment can use a different host and path, such as `https://komashi-app.test/api/v1`. Use the value displayed in your environment instead of adding `/api` or `/v1` by guesswork.

The **Documentation** link opens the interactive OpenAPI reference. Use it for each operation's exact request fields, response schema and validation rules.

## Authenticate a request

Create a provider key under [Integration → API](https://komashi.com/docs/provider/Settings/10api/index.md). Send it in the `Authorization` header, and request JSON responses:

```bash
export KOMASHI_API_BASE='https://api.komashi.com/v1'
export KOMASHI_API_KEY='YOUR_PROVIDER_API_KEY'

curl "$KOMASHI_API_BASE/services" \
  -H "Authorization: Bearer $KOMASHI_API_KEY" \
  -H 'Accept: application/json'
```

Use a backend environment variable or secret store for the real key. It identifies the provider, so ordinary provider-key requests do not need a separate provider ID header.

The key must have the ability required by the operation. Read access is sufficient for this first request. Checkout creation requires **Create**; usage updates and subscription cancellation require **Update**. Do not infer the permission solely from the HTTP method.

## Read JSON responses

Responses include these common fields:

```json
{
  "succeeded": true,
  "status_code": 200,
  "data": []
}
```

`data` contains the result. Depending on the operation, it can be a resource, an array, a checkout result or `null`. Additional fields such as `message`, validation `errors`, `links` and `meta` can appear alongside the common fields. Endpoint errors carry a stable `code` field, for example `SERVICE_NOT_FOUND` or `CATALOG_CHANGE_INVALID`. Branch on `code` rather than on the `message` text, which can be localized or reworded. Generic errors, such as an authentication failure or a rate limit, may not include `code`; check the HTTP status as well.

A successful operation that internally produces `204 No Content` is returned as HTTP `200` with `data: null`. Check the HTTP status and the response fields rather than expecting a 204 for every deletion.

### Pagination

Paginated list operations accept `page` and `per_page`. The default page size is 25, and the maximum is 100. Preserve pagination information such as `links` and `meta` when it is present.

```bash
curl "$KOMASHI_API_BASE/customers?page=1&per_page=25" \
  -H "Authorization: Bearer $KOMASHI_API_KEY" \
  -H 'Accept: application/json'
```

## Discover the catalog

Paths below are relative to the versioned API endpoint.

| Request | Purpose |
| --- | --- |
| `GET /services` | List services. |
| `GET /services/{service}` | Get one service. |
| `GET /services/{service}/plans` | List its plans. |
| `GET /services/{service}/plans/{plan_id}/periods` | List a plan's periods. |
| `GET /services/{service}/addons` | List add-ons. |
| `GET /services/{service}/addons/{addon_id}/options` | List add-on options. |
| `GET /services/{service}/service-prices/discounts` | Read available discounts. |
| `GET /services/{service}/catalog` | Read the service's whole catalog in one response. |

Fetch the service, then its plans, periods, add-ons and options. Use identifiers from these results.

### Change the catalog

Plans, periods and add-ons can be changed in two steps:

| Request | Purpose | Key ability |
| --- | --- | --- |
| `GET /services/{service}/catalog` | Read the current catalog. | **Read** |
| `POST /services/{service}/catalog/preview` | Validate a change and return its diff, warnings and a `change_token`. Writes nothing. | **Update** |
| `POST /services/{service}/catalog/apply` | Apply a previewed change with its `change_token`. This changes the live catalog. | **Update** |

The change token is valid for 30 minutes. Apply returns `409` if the catalog changed since the preview (run the preview again) and `410` if the token has expired. The service currency cannot be changed. An applied change sends the same plan, period and add-on webhook events as a change made in the Control Center; the preview sends none. Coupon discounts are still configured in the Control Center.

## Connect customers and subscriptions

The API supports customer list, detail, create, update and delete operations under `/customers`, and contact operations under `/customers/{customer_id}/contacts`. Deleting a customer is refused with `CUSTOMER_HAS_PAYMENT_IN_PROGRESS` while one of their payments is still in progress; try again once it has finished.

Creating a customer requires billing details, including `billing_name`, `billing_address`, `billing_zip`, `billing_city` and `billing_country`. Depending on the country, a billing state is also required. Provider-defined required customer fields still apply. See the OpenAPI reference before assembling the request.

To create a subscription checkout, send `POST /services/{service}/subscriptions`. The following is an illustrative request body; replace every identifier with a real value from your provider:

```json
{
  "customer_id": "CUSTOMER_UUID",
  "plan_id": "PLAN_UUID",
  "period_id": "PERIOD_UUID",
  "quantity": 1,
  "external_reference": "your-internal-order-reference",
  "return_url": "https://your-app.example/subscription/return",
  "cancel_url": "https://your-app.example/subscription/cancel"
}
```

Send JSON with `Content-Type: application/json`. The response's `data` includes `checkout_url`, `subscription_id`, `group_id` and the created subscription items. Direct the customer to `checkout_url` to continue the checkout. The return URL tells you where the browser returned; confirm the current subscription state before granting paid access.

The created subscription is a draft (`is_draft: true`) until it is paid. To discard an unpaid draft right away, send `DELETE /subscriptions/{subscription_id}/draft`; this deletes it immediately and sends a `subscription.delete` webhook. Otherwise an unpaid draft is deleted automatically 24 hours after creation. No webhook is sent for this automatic deletion, so do not rely on one; create a new checkout if the customer returns later.

| Request | Purpose |
| --- | --- |
| `GET /services/{service}/subscriptions` | List subscriptions for a service. |
| `GET /subscriptions/{subscription_id}` | Read one subscription. |
| `GET /subscriptions/groups/{group_id}` | Read a subscription group. |
| `POST /subscriptions/{subscription_id}/payment-link` | Generate an available payment link for an existing subscription. |
| `POST /subscriptions/{subscription_id}/change-plan` | Request a plan change using the target configuration. |
| `PATCH /subscriptions/{subscription_id}/dates` | Update supported lifecycle dates. |
| `PATCH /subscriptions/{subscription_id}/status` | Update the status and optional renewal setting. |
| `DELETE /subscriptions/{subscription_id}/cancel` | Cancel a subscription or a pending plan change. |
| `DELETE /subscriptions/{subscription_id}/draft` | Delete an unpaid draft immediately; sends `subscription.delete`. |
| `GET /invoices` and `GET /invoices/{invoice_id}` | Read invoice records. |

Cancellation follows the provider's cancellation settings. A group ID cannot replace the individual subscription ID required by these operations. Plan-change and payment-link operations have their own business-state requirements; inspect the reference and returned errors.

## Record usage

Read `GET /subscriptions/{subscription_id}/usage` before relying on an allowance. Group usage is available through `GET /subscriptions/groups/{group_id}/usage`.

For `POST /subscriptions/{subscription_id}/sync-usage`, send the **new absolute total**:

```json
{ "usage": 125 }
```

For `POST /subscriptions/{subscription_id}/adjust-usage`, send an increment or decrement:

```json
{ "action": "increase", "value": 1 }
```

Supported actions are `increase` and `decrease`; omitting `value` uses 1. Repeating an adjustment applies it again, so do not blindly retry a write after a network timeout. Read the current state first when the result is uncertain.

## Errors and rate limiting

| Status | What to check |
| --- | --- |
| `401` | Missing, invalid or deleted key. |
| `403` | Wrong token type or insufficient ability. |
| `404` | Unknown record or a record outside the current provider. |
| `409` | A business-state conflict, such as a draft, an incompatible pending operation, or a catalog that changed since its preview. |
| `410` | The catalog `change_token` has expired or does not exist; run the preview again. |
| `422` | Invalid fields, unsupported values or a business validation failure. |
| `429` | Request limit reached; wait and retry with backoff. |
| `5xx` | Server-side failure; preserve enough context to investigate before repeating a write. |

The configured REST limit is 100 requests per minute. Handle `429` responses even if you believe your own integration is below the limit.

Related: [API keys](https://komashi.com/docs/provider/Settings/10api/index.md), [webhooks](https://komashi.com/docs/provider/Settings/11webhooks/index.md), [MCP integration](https://komashi.com/docs/provider/Developers/mcp-integration/index.md).
