# API quick recipes: copy, paste, run

_API · 7 min read · Updated 2026-10-22 · https://lanachat.ai/guides/api-recipes/_

Eight complete recipes for the jobs agencies automate first. Each one is ready to copy, in cURL and in JavaScript (Node 20 or later, no library). Set your token once, as in the [quick start](https://lanachat.ai/guides/api-overview/), and every block runs as it is.

```bash
export LANACHAT_TOKEN="lc_live_8f2c1d0e9b7a4c6f"
```

The JavaScript recipes use this small helper. Paste it once at the top of your file:

```js
const API = 'https://api.lanachat.ai/v1';

async function lanachat(method, path, body) {
  const res = await fetch(API + path, {
    method,
    headers: {
      Authorization: `Bearer ${process.env.LANACHAT_TOKEN}`,
      'Content-Type': 'application/json',
      ...(method === 'POST' ? { 'Idempotency-Key': crypto.randomUUID() } : {}),
    },
    body: body ? JSON.stringify(body) : undefined,
  });
  const data = await res.json();
  if (!res.ok) throw new Error(`${res.status} ${data.error.code}: ${data.error.message}`);
  return data;
}
```

## 1. Add a creator and connect her Telegram account

Needs `can_setup`, and an **owner** token for the licence (step 4). Create the creator, fill her identity, add her account with its bot token, then give the account a licence so it can reply.

```bash
# 1. the creator
curl -X POST https://api.lanachat.ai/v1/creators \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"name": "Lea", "timezone": "Europe/Paris"}'

# 2. her identity (she must be 18 or older)
curl -X PUT https://api.lanachat.ai/v1/creators/cr_lea/identity \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Content-Type: application/json" \
  -d '{"first_name": "Lea", "date_of_birth": "2001-04-12", "nationality": "FR", "languages": ["fr", "en"]}'

# 3. her Telegram account
curl -X POST https://api.lanachat.ai/v1/accounts \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"creator_id": "cr_lea", "bot_token": "<bot token>", "market": "fr", "traffic": "instagram"}'

# 4. a licence from your stock
curl -X POST https://api.lanachat.ai/v1/billing/licences/assign \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"account_id": "acc_1001"}'
```

```js
const creator = await lanachat('POST', '/creators', { name: 'Lea', timezone: 'Europe/Paris' });
await lanachat('PUT', `/creators/${creator.id}/identity`, {
  first_name: 'Lea', date_of_birth: '2001-04-12', nationality: 'FR', languages: ['fr', 'en'],
});
const account = await lanachat('POST', '/accounts', {
  creator_id: creator.id, bot_token: process.env.BOT_TOKEN, market: 'fr', traffic: 'instagram',
});
await lanachat('POST', '/billing/licences/assign', { account_id: account.id });
console.log(account.id, account.health);   // "pending" until she confirms in Telegram
```

The account stays `pending` until the creator confirms the business connection in her Telegram settings. A [webhook](https://lanachat.ai/guides/api-webhooks/) tells you when it turns `connected`: the event `account.health_changed`. See [Telegram accounts](https://lanachat.ai/guides/api-telegram-accounts/) and [Billing](https://lanachat.ai/guides/api-billing/).

## 2. Read today's sales

Needs `can_sales_read`. `/stats` gives the totals of a day, per link and per source; Stars count at $0.013 each in net sales.

```bash
TODAY=$(date -u +%F)
curl "https://api.lanachat.ai/v1/stats?from=$TODAY&to=$TODAY" \
  -H "Authorization: Bearer $LANACHAT_TOKEN"
```

```js
const today = new Date().toISOString().slice(0, 10);
const stats = await lanachat('GET', `/stats?from=${today}&to=${today}`);
const stars = stats.days.reduce((sum, d) => sum + d.revenue_stars, 0);
console.log(`${stars} Stars today, about $${(stars * 0.013).toFixed(2)}`);
for (const l of stats.links) console.log(l.link_id, l.buyers, 'buyers', l.revenue_stars, 'Stars');
```

Add `creator_id=cr_lea` for one creator. Days are UTC days. To get each sale the moment it happens, use recipe 6.

## 3. Send a message to a fan

Needs `can_chat`. Find the conversation, then send. A message sent through the API counts as sent by a person and takes the conversation over until you give it back (recipe 4).

```bash
# conversations of Lea with unread messages
curl "https://api.lanachat.ai/v1/conversations?creator_id=cr_lea&unread=true" \
  -H "Authorization: Bearer $LANACHAT_TOKEN"

# send a text and a paid photo
curl -X POST https://api.lanachat.ai/v1/conversations/conv_1001_88231/messages \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"text": "Only for you, tonight", "media": [{"id": "med_5522", "stars": 250}]}'
```

```js
const { data: convs } = await lanachat('GET', '/conversations?creator_id=cr_lea&unread=true');
const conv = convs[0];
const msg = await lanachat('POST', `/conversations/${conv.id}/messages`, {
  text: 'Only for you, tonight',
  media: [{ id: 'med_5522', stars: 250 }],
});
console.log(msg.status);   // "queued", then "sent" and "read"
```

Add `"send_at"` (ISO 8601, up to 7 days ahead) to schedule it. See [Conversations](https://lanachat.ai/guides/api-conversations/).

## 4. Take over a conversation, then give it back to the AI

Needs `can_chat`. While a person holds the conversation, the AI does not answer this fan.

```bash
curl -X POST https://api.lanachat.ai/v1/conversations/conv_1001_88231/takeover \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)"

# ... your team replies ...

curl -X POST https://api.lanachat.ai/v1/conversations/conv_1001_88231/release \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)"
```

```js
const id = 'conv_1001_88231';
await lanachat('POST', `/conversations/${id}/takeover`);   // state: "human"
// ... your team replies ...
await lanachat('POST', `/conversations/${id}/release`);    // back to the AI
```

A conversation stopped for a safety reason cannot be given back to the AI: the API answers `409`.

## 5. Create a tracking link

Needs `can_tracking`. One link per ad or per bio, so you know which one sells.

```bash
curl -X POST https://api.lanachat.ai/v1/links \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"name": "Insta bio · Lea", "slug": "lea-bio", "creator_id": "cr_lea", "source": "instagram", "destination": "pool"}'
```

```js
const link = await lanachat('POST', '/links', {
  name: 'Insta bio · Lea', slug: 'lea-bio', creator_id: 'cr_lea', source: 'instagram', destination: 'pool',
});
console.log(link.public_url);   // put this URL in her bio
```

Add `"domain"` to use one of your verified domains. Read its clicks, conversations and buyers with `GET /links/{id}` or `/stats`. See [Tracking](https://lanachat.ai/guides/api-tracking/).

## 6. Get every sale on your server, as it happens

Needs an **owner** token to add the endpoint. LanaChat sends a signed `POST` for each verified sale and each refund.

```bash
curl -X POST https://api.lanachat.ai/v1/webhooks \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"url": "https://hooks.mia-agency.com/lanachat/webhook", "events": ["sale.completed", "sale.refunded"]}'
```

```js
import express from 'express';
import { createHmac, timingSafeEqual } from 'node:crypto';

// once: add the endpoint (keep the secret of the answer as LANACHAT_WEBHOOK_SECRET)
// await lanachat('POST', '/webhooks', { url: 'https://hooks.mia-agency.com/lanachat/webhook', events: ['sale.completed', 'sale.refunded'] });

// the receiver: check the signature, answer 200 fast, then work

function verify(raw, header, secret) {
  const parts = (header || '').split(',').map((p) => p.trim().split('='));
  const t = parts.find(([k]) => k === 't')?.[1];
  if (!t || !(Math.abs(Date.now() / 1000 - Number(t)) < 300)) return false;
  const want = createHmac('sha256', secret).update(t + '.').update(raw).digest('hex');
  return parts.some(([k, v]) => k === 'v1' && v.length === want.length && timingSafeEqual(Buffer.from(v), Buffer.from(want)));
}

const app = express();
app.post('/lanachat/webhook', express.raw({ type: 'application/json' }), (req, res) => {
  if (!verify(req.body, req.get('LanaChat-Signature'), process.env.LANACHAT_WEBHOOK_SECRET)) return res.sendStatus(400);
  res.sendStatus(200);
  const event = JSON.parse(req.body);
  if (event.type === 'sale.completed') console.log('Sale:', event.data.stars, 'Stars,', event.data.creator_id, event.data.sent_by);
  if (event.type === 'sale.refunded') console.log('Refund of', event.data.sale_id);
});
app.listen(3000);
```

The signing secret comes once, in the answer of the first call: store it as `LANACHAT_WEBHOOK_SECRET`. Press **Test** (`POST /webhooks/{id}/test`) to receive a `ping`. Python, retries and every event are in [Webhooks](https://lanachat.ai/guides/api-webhooks/).

## 7. Upload a photo to the Vault and price it

Needs `can_setup`. Ask for an upload URL, send the file there, confirm, then set its price.

```bash
# 1. upload URL
curl -X POST https://api.lanachat.ai/v1/media/uploads \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"creator_id": "cr_lea", "file_name": "beach-01.jpg", "content_type": "image/jpeg", "size_bytes": 2483201}'

# 2. the file, to the "url" of the answer (no Authorization header there)
curl -X PUT "<url from step 1>" -H "Content-Type: image/jpeg" --data-binary @beach-01.jpg

# 3. confirm, 4. price it
curl -X POST https://api.lanachat.ai/v1/media/uploads/up_7781/complete \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)"
curl -X PATCH https://api.lanachat.ai/v1/media/med_5521 \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Content-Type: application/json" \
  -d '{"access": "paid", "stars": 300, "description": "Beach, golden hour, white bikini"}'
```

```js
import { readFile, stat } from 'node:fs/promises';

const file = 'beach-01.jpg';
const up = await lanachat('POST', '/media/uploads', {
  creator_id: 'cr_lea', file_name: file, content_type: 'image/jpeg', size_bytes: (await stat(file)).size,
});
const put = await fetch(up.url, { method: up.method, headers: up.headers, body: await readFile(file) });
if (!put.ok) throw new Error('Upload failed: ' + put.status);
const media = await lanachat('POST', `/media/uploads/${up.upload_id}/complete`);
await lanachat('PATCH', `/media/${media.id}`, { access: 'paid', stars: 300, description: 'Beach, golden hour, white bikini' });
```

The media is `processing`, then `ready`: the `media.ready` webhook tells you when. See [Vault](https://lanachat.ai/guides/api-vault/).

## 8. Change a brain, test it, publish it

Needs `can_engine`, and `can_publish` for the last step. The live brain only changes on publish.

```bash
curl -X PATCH https://api.lanachat.ai/v1/brains/br_lea_main/draft \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Content-Type: application/json" \
  -d '{"settings": {"first_offer_after_messages": 6}}'

curl -X POST https://api.lanachat.ai/v1/playground/br_lea_main/message \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)" -H "Content-Type: application/json" \
  -d '{"text": "hey, are you real?", "fan": {"language": "en", "is_new": true}}'

curl -X POST https://api.lanachat.ai/v1/brains/br_lea_main/publish \
  -H "Authorization: Bearer $LANACHAT_TOKEN" -H "Idempotency-Key: $(uuidgen)"
```

```js
await lanachat('PATCH', '/brains/br_lea_main/draft', { settings: { first_offer_after_messages: 6 } });
const test = await lanachat('POST', '/playground/br_lea_main/message', {
  text: 'hey, are you real?', fan: { language: 'en', is_new: true },
});
console.log(test.reply.text, '|', test.explain);
// happy with the answer? publish
const live = await lanachat('POST', '/brains/br_lea_main/publish');
console.log('Live version', live.version);
```

The Playground never writes to a fan and never sells. See [AI Brains](https://lanachat.ai/guides/api-ai-brains/).

## Related

- [LanaChat API: quick start, authentication and errors](https://lanachat.ai/guides/api-overview/)
- [API: webhooks, events and signatures](https://lanachat.ai/guides/api-webhooks/)
- [API & MCP: connect an AI assistant](https://lanachat.ai/guides/api-mcp/)
