# Klupt API: notes for coding agents

This project uses the Klupt API (public Instagram data as JSON). Follow these notes when you write code that calls it.
Full docs: https://api.klupt.io/llms-full.txt · OpenAPI 3.1: https://api.klupt.io/openapi.json · Human docs: https://api.klupt.io/docs

## Rules

1. **The API key is a server-side secret.** Read it from the environment variable `KLUPT_API_KEY`. Never put it in browser or mobile code, in the repository, or in logs. Browser code calls our own backend route, and that route calls the Klupt API.
2. The backend route is not an open proxy: allow only the endpoints this app uses and validate input (usernames match `^[A-Za-z0-9._]{1,30}$`).
3. Every call costs credits (see below). Fetch only the pages the app needs, and cache results in our own storage when the same data is read again.
4. Keys starting with `kd_test_` return sample data at no cost; `kd_live_` keys return real data and spend credits. The code must work the same with both. Never hard-code sample data.

## Basics

- Base URL: `https://api.klupt.io/v1`
- Auth: header `X-Api-Key: <KLUPT_API_KEY>` on every request. HTTPS GET, JSON responses.
- Success: `{ "data": ..., "pagination"?: { "next_cursor", "has_more", "page_size" }, "meta": { "request_id", "cached", "credits_charged", "credits_remaining", ... } }`
- If `meta.warning` is present, read it: it means the integration does something unsafe (for example a live key sent from a browser).
- Error: `{ "error": { "code", "message", ... } }` with an HTTP status. Failed calls cost nothing.
- Pagination: pass `pagination.next_cursor` back as `?cursor=` until it is `null`. Cursors are opaque; never build or edit them. Each page is billed.
- `followers` and `following` are newest first: `position` 1 is the most recent follow. Instagram gives no follow dates; to date new follows, store snapshots and compare.
- `{user}` is a numeric id or a username. A username we have to look up costs 1 extra credit, so store and send the numeric `id` once you have it. Write `@12345` for an all-digit username, or add `?by=username|id`.
- Public accounts only. A private account answers `403 private_account` (free).

## Errors to handle

- `401 missing_api_key` / `invalid_api_key`: configuration problem; do not retry.
- `402 insufficient_credits`: the account is out of credits; show a clear message, do not retry.
- `403 private_account`, `404 user_not_found`: show a friendly message.
- `429 rate_limited` / `concurrency_limited`: wait for the `Retry-After` header (seconds), then retry. Keep requests sequential or in a small queue.
- `502 upstream_unavailable`, `503 service_busy`, `504 upstream_timeout`: retry with backoff (at most 3 tries).
- Full list: https://api.klupt.io/docs/errors.md

## Prices

1 credit per successful call; a following page (about 200 accounts) is 2 credits. Cache hits and errors are free. Live data is cached for 60 seconds, so polling more often than once a minute returns the cached answer. Details: https://api.klupt.io/pricing.md

## Endpoints

- `GET /v1/users/by-username/{username}`: Profile by username. The full public profile, including the numeric user id. 1 credit.
- `GET /v1/users/by-id/{id}`: Profile by user id. The full public profile for a numeric user id. 1 credit.
- `GET /v1/users/{user}`: Profile by username or id. Same as the two routes above; accepts either. 1 credit.
- `GET /v1/users/{user}/followers`: Followers, newest first. Up to 25 accounts a page. 1 credit a page.
- `GET /v1/users/{user}/following`: Following, newest first. Up to about 200 accounts a page, newest first. 2 credits a page (free from cache).
- `GET /v1/users/{user}/posts`: Recent posts. Newest posts first, about 12 a page. 1 credit a page.
- `GET /v1/search/users`: Search accounts by name. Up to about 40 results per search. 1 credit a page.
- `GET /v1/users/{user}/reels`: Reels of an account, newest first (or most viewed). 1 credit a page. Page size about 12.
- `GET /v1/users/{user}/tagged`: Posts the account is tagged in. 1 credit a page. Page size about 12.
- `GET /v1/users/{user}/reposts`: Posts the account reposted. 1 credit a page. Page size about 12.
- `GET /v1/users/{user}/stories`: Live stories (last 24 hours). 1 credit a page.
- `GET /v1/users/{user}/highlights`: Story highlights of an account. 1 credit a page.
- `GET /v1/users/{user}/account-details`: When the account joined, its country and username changes. 1 credit.
- `GET /v1/users/{user}/related`: Accounts similar to this one. 1 credit a page. Page size up to about 80.
- `GET /v1/posts/stats`: Like, comment and view counts for up to 20 posts in one call. 1 credit.
- `GET /v1/posts/{post}`: One post or reel by shortcode, id or URL. 1 credit.
- `GET /v1/posts/{post}/comments`: Comments on a post. 1 credit a page. Page size about 15.
- `GET /v1/posts/{post}/likers`: Accounts that liked a post. 1 credit a page. Page size about 100.
- `GET /v1/comments/{comment}/replies`: Replies to a comment. 1 credit a page.
- `GET /v1/comments/{comment}/likers`: Accounts that liked a comment. 1 credit a page.
- `GET /v1/stories/{story}`: One story by id. 1 credit.
- `GET /v1/highlights/{highlight}`: The stories inside a highlight. 1 credit a page.
- `GET /v1/search/hashtags`: Find hashtags. 1 credit a page.
- `GET /v1/search/places`: Find places by name or near a point. 1 credit a page.
- `GET /v1/search/audio`: Find songs and original audio. 1 credit a page.
- `GET /v1/hashtags/{tag}`: A hashtag and its post count. 1 credit.
- `GET /v1/hashtags/{tag}/posts`: Top posts for a hashtag. 1 credit a page.
- `GET /v1/places/{place}`: A place: name, category, address, contact. 1 credit.
- `GET /v1/places/{place}/posts`: Top or recent posts tagged at a place. 1 credit a page.
- `GET /v1/audio/trending`: Trending audio on Reels. 1 credit a page.
- `GET /v1/audio/{audio}`: A song or original audio. 1 credit.
- `GET /v1/audio/{audio}/reels`: Reels that use an audio. 1 credit a page.
- `GET /v1/resolve`: Instagram URL → type and ids (free)
- `GET /v1/account`: Your plan, balance and limits (free)

Each endpoint has a page with parameters and a sample response: https://api.klupt.io/llms.txt lists them all as Markdown.

## Example (server-side, Node.js)

```js
const res = await fetch(`https://api.klupt.io/v1/users/${encodeURIComponent(username)}/following`, {
  headers: { "X-Api-Key": process.env.KLUPT_API_KEY },
});
const body = await res.json();
if (!res.ok) throw new Error(`Klupt ${body.error.code}: ${body.error.message}`);
const newest = body.data.slice(0, 20); // position 1 = most recent follow
```
