# REST API

> Base URL, authentication, discovery, cost and idempotency headers, paging, async jobs and errors for threefold's HTTP API.

The threefold REST API at `https://api.threefold.eco` serves the full market-entry library, deterministic ASO/SEO/GEO and i18n checks, and workspace tracking over plain JSON with an OpenAPI 3.1 contract. Every callable route is discoverable at `/api/discover`, reports exact micro-USD cost headers, supports idempotent retries, and works keyless for read-only library lookups.

## Base URL and spec

```text
https://api.threefold.eco
```

- OpenAPI 3.1, generated from the endpoint registry: <https://api.threefold.eco/openapi.json>
- Rendered on this site: [API reference](https://threefold.eco/docs/api/reference.md)
- For agents: <https://api.threefold.eco/llms.txt>, generated from the same registry

## Authentication

```sh
curl https://api.threefold.eco/api/usage -H "Authorization: Bearer tf_live_your_key"
```

| Credential | Notes |
|---|---|
| API key, `tf_live_…` | Created in your workspace with scopes `read`, `track`, `ingest`, `analysis`, `admin`. Only a hash is stored; the key is shown once. |
| Sandbox key, `tf_test_…` | Fixture data only, read-only, never charged, never calls a supplier |
| OAuth 2.1 access token | From browser sign-in, as used by chat apps and `npx threefold login` |
| None | Keyless library reads, capped per IP address per day (50 by default). Responses carry `X-Threefold-Next: sign in for the monthly allowance`. |

`GET` routes need the `read` scope. Writes need the scope named on each route. See [Accounts and keys](https://threefold.eco/docs/accounts.md).

## Discover, describe, call

Every callable endpoint is in the registry with its task, inputs, coverage, provenance, limits and cost class:

```sh
curl "https://api.threefold.eco/api/discover?q=compare+meditation+apps+in+DE+and+JP"
curl https://api.threefold.eco/api/discover/compare_markets
curl "https://api.threefold.eco/api/compare?from=de&to=jp&niche=health-fitness"
```

`/api/discover` ranks endpoints for a plain-language task and lists valid market codes and niche slugs. `/api/discover/<id>` returns one endpoint's inputs with examples, coverage, provenance, limits and price.

## Common routes

| Route | What |
|---|---|
| `GET /api/meta` | Markets, niches, language variants, the library configuration, and when each source was last collected ([freshness](https://threefold.eco/docs/data.md#how-fresh-the-data-is)) |
| `GET /api/chart?market&niche&store` | Top apps in one market and niche, with localisation signals |
| `GET /api/compare?from&to&niche&store` | One niche in two markets: shared, not crossed over, local only |
| `GET /api/apps/:id?markets&store` | One app across countries' stores, both stores side by side when linked |
| `GET /api/apps/:id/brief?to&from&store` | Market brief for one app into one market |
| `GET /api/search?q&limit&offset&store` | Title and developer search over both stores |
| `GET /api/apps/:id/history?market&store` | Listing changes over time |
| `GET /api/chart-history?app&market&niche&store&days` | Daily chart ranks |
| `GET /api/apps/:id/reviews`, `/reviews/summary` | Store reviews and their language mix |
| `GET /api/paid`, `/api/paid/summary`, `/api/organic` | Collected ads and posts (Paid and Organic need supplier connections; see [Data and freshness](https://threefold.eco/docs/data.md)) |
| `GET /api/visuals/gallery` | [Screenshot gallery](https://threefold.eco/docs/visuals.md) |
| `GET /api/market-glossary`, `POST /api/variant-lint`, `POST /api/listing-check`, `GET /api/price-points`, `GET /api/market-launch-checklist` | [Market resources](https://threefold.eco/docs/resources.md) |
| `GET /api/i18n/rules`, `POST /api/i18n/reports` | [i18n check](https://threefold.eco/docs/i18n-check.md) catalogue and stored reports |
| `POST /api/watches`, `GET /api/watches` | [Watches](https://threefold.eco/docs/watches.md) |
| `GET /api/usage`, `GET /api/me` | Plan, limits and usage |

Markets are `gb`, `us`, `es`, `mx`, `ar`, `br`, `de`, `fr`, `jp`, `kr`. Niches are `games`, `education`, `health-fitness`, `finance`, `productivity`, `lifestyle`. `store` is `ios` (default) or `play`. The full list is in the [API reference](https://threefold.eco/docs/api/reference.md).

## Response headers

| Header | Meaning |
|---|---|
| `X-Threefold-Cost-Micro-Usd` | What this call cost (1,000,000 micro-USD is one US dollar) |
| `X-Threefold-Balance-Micro-Usd` | Workspace balance after the call, when there is one |
| `X-Threefold-Idempotent-Replay` | `true` when the answer is a replay of an earlier call with the same `Idempotency-Key` |
| `X-Threefold-Next` | A hint for keyless callers |
| `Retry-After` | On `429`: seconds to wait |

Send `Idempotency-Key: <unique value>` on a call you might retry. A settled call with the same key returns the earlier result without charging again; a call with that key still in flight gets `409`.

## Paging

List routes take `limit` and `offset`. `/api/paid`, `/api/organic` and `/api/chart-history` return `page: { limit, offset, nextOffset }`, where `nextOffset` is `null` on the last page. `/api/search` returns a plain array: a page shorter than `limit` (default 20, max 100) is the last one.

## Intent inputs

Discovery and the heavier endpoints accept `task_intent` (what you are trying to do; logged hashed) and `output_destination`: `code` for compact JSON, `doc` for citations and links, `chat` for a summary sentence, or `design_tool`.

## Async jobs and webhooks

Fresh pulls and backfills return a job id. Read it at `GET /api/jobs/:id` until it finishes. Its `state` is `queued`, `retrying` (a failed attempt waits until `run_after`), `running`, `succeeded`, `failed`, `skipped` or `cancelled`, and `next_actions` lists what you may call next. `POST /api/jobs/:id/cancel` (the `ingest` scope) stops a queued or retrying job. These requests send no callback, so they refuse `webhook_url`. Watches do send webhooks: the body is signed with HMAC-SHA256 in `X-Threefold-Signature` (`sha256=<hex>`), and webhook URLs must be public addresses.

## Asking for coverage

`POST /api/coverage/request` records a market, app or channel you needed and threefold did not have. It is free, and it is how missing coverage gets prioritised.

## Errors

Every error has the same body: `{ error, code, whose_error, retryable, hint }`. The codes are a closed set: see [Errors](https://threefold.eco/docs/errors.md).

## Questions

### How can an agent or developer find the right REST endpoint?

Call `GET https://api.threefold.eco/api/discover?q=<plain-language task>` to rank matching endpoints, then call `GET /api/discover/<id>` to read exact inputs, examples, coverage limits and cost class. You can also download the full OpenAPI 3.1 spec at `https://api.threefold.eco/openapi.json` or read `/llms.txt`.

### Do I need an API key to test the REST API?

No. Read-only library routes such as `/api/compare`, `/api/chart`, `/api/apps/:id` and `/api/search` work without credentials up to 50 keyless requests per IP address per day. For automated tests and CI, use a `tf_test_…` sandbox key for fixture responses at zero cost.

### How does idempotency prevent duplicate charges on retries?

Include an `Idempotency-Key` header with a unique token on any request you might retry. If the earlier call already settled, the API replays that response with `X-Threefold-Idempotent-Replay: true` and zero additional charge; if it is still running, the server answers `409`.
