# Lipi Studio integration spec

Paste this into your AI to build a receiver or a reader. Base URL: the address you
use for Lipi Studio, written `$STUDIO` below (the host, such as `https://studio.lipi.com.np`). REST paths start with `/api`; `/mcp`, `/oauth/*` and `/.well-known/*` are at the root.

## 1. Webhooks you receive

Two senders use the same signed format.

**Event subscriptions** (workspace level) send `item.published`, `item.failed`,
`item.approved` and `item.needs_review`:

```json
{
  "type": "item.published",
  "timestamp": "2026-11-23T09:00:00+00:00",
  "data": {
    "workspace_id": "uuid",
    "workspace_id": "uuid",
    "item": { "id": "uuid", "type": "article", "title": "text", "status": "published", "version": 3 },
    "detail": { "target_id": "uuid", "method": "studio", "url": "https://...", "external_id": "text" }
  }
}
```

`detail` holds the transition's own facts: for `item.published` the target, method, URL and
external id; for `item.failed` the `reason`; for `item.approved` and `item.needs_review` the
previous status in `from` and the reviewer's `note`.

**A webhook publish target** sends the content itself, once per approved version:

```json
{
  "type": "item.published",
  "timestamp": "2026-11-23T09:00:00+00:00",
  "data": {
    "item": { "id": "uuid", "type": "article", "title": "text" },
    "version": { "number": 3, "body": { "title": "...", "markdown": "...", "seo_description": "...", "slug": "..." } }
  }
}
```

The `body` has the shape of the item's content type (`article`: `title`, `markdown`,
`seo_description`, `slug`; `social_post`: `text`, `variants[{network, text}]`, `link`). Ask
`GET $STUDIO/api/types` for an example of each.

### Verify the signature

Headers: `webhook-id`, `webhook-timestamp` (Unix seconds), `webhook-signature`
(`v1,<base64>`, several may be listed, space separated).

1. Read the raw request body bytes. Do not parse and re-serialize it first.
2. Reject if `webhook-timestamp` is more than 5 minutes from your clock.
3. `signed = webhook-id + "." + webhook-timestamp + "." + body`.
4. Key: if your secret starts with `whsec_`, base64-decode the rest; otherwise use the
   secret's bytes as they are.
5. `expected = base64(HMAC_SHA256(key, signed))`. Compare `"v1," + expected` with each listed
   signature in constant time. Any match is valid.
6. Answer with any `2xx` once stored. Anything else is retried (up to 5 tries, backing off from
   1 minute to hours). `webhook-id` stays the same across retries: use it to drop repeats.

```python
import base64, hashlib, hmac, time

def valid(secret: str, headers: dict, body: bytes) -> bool:
    ts = headers["webhook-timestamp"]
    if abs(time.time() - int(ts)) > 300:
        return False
    key = base64.b64decode(secret[6:]) if secret.startswith("whsec_") else secret.encode()
    signed = f'{headers["webhook-id"]}.{ts}.'.encode() + body
    mine = "v1," + base64.b64encode(hmac.new(key, signed, hashlib.sha256).digest()).decode()
    return any(hmac.compare_digest(mine, s) for s in headers["webhook-signature"].split(" "))
```

You choose the secret (for example `whsec_` plus `openssl rand -base64 32`). Lipi Studio stores it
sealed and never shows it again, only its last 4 characters.

## 2. Hosted blog (read API)

Public, read-only, no sign-in. Only published articles appear, never drafts. Responses carry
`ETag` and `Cache-Control: public, max-age=60`; send `If-None-Match` to get `304`.

| Route | Returns |
|---|---|
| `GET $STUDIO/api/public/blogs/{slug}/posts?limit=20&offset=0` | `{ blog: {slug, title, description, url}, posts: [{slug, title, excerpt, url, published_at, updated_at}] }`, newest first, `limit` up to 100 |
| `GET $STUDIO/api/public/blogs/{slug}/posts/{post}` | the summary plus `markdown`, `html` and `json_ld` (a schema.org `Article`) |
| `GET $STUDIO/api/public/blogs/{slug}/rss.xml` | RSS 2.0, latest 50 |
| `GET $STUDIO/api/public/blogs/{slug}/sitemap.xml` | sitemap, up to 500 posts |

`html` is safe to embed: raw HTML in the Markdown is escaped, and links other than http(s),
`mailto:`, `tel:` and relative ones become `#`. The raw `markdown` is as written: sanitise it
yourself if you render it. If you put `json_ld` inside a `<script>` tag, escape `<` in it.
Unknown blog or post: `404`.

## 3. MCP (agents)

Endpoint: `POST $STUDIO/mcp` (Streamable HTTP), `Authorization: Bearer lst_...` (an API token
from your account; use an agent token for autonomous agents). Start with `list_workspaces`.

Publishing tools: `list_publish_targets`, `publish_now`, `get_posting_packet`, `mark_posted`,
`report_post_failed`, `publish_manual` (people only), `list_publish_jobs`. An agent that posts
for you calls `get_posting_packet(item_id, target_id)`, posts with its own session, then
`mark_posted(item_id, target_id, url)` (or `report_post_failed`). Each publish method works only
when its toggle is on (`get_automation`). The packet's `assets` are the item's uploaded files,
each with a signed download URL valid one hour.

Uploads: `create_upload(workspace_id, filename, content_type, size, item_id?)` returns a pending
asset and a signed PUT URL (15 minutes). PUT exactly `size` bytes with that `Content-Type`, then
`complete_upload(asset_id)` returns the ready asset with a signed download URL. Images (PNG, JPEG,
WebP, GIF), video (MP4, QuickTime, WebM), audio (MP3, M4A, WAV), PDF and captions (WebVTT, SRT),
up to 200 MiB.

The same actions exist as REST: see `$STUDIO/api/openapi.json`. Errors are
`{"error": {"code", "message"}}`; an id from another workspace answers `404`.
