# Quickstart

Make your first request to the Talksign API and get a signed ASL video back in a few minutes.

## 1. Get an API key

Sign in to the developer console (https://developer.talksign.co) and create a key under **API keys**. A test key (`sk-test-`) is free and returns sample responses. Add funds to your wallet and create a live key (`sk-live-`) when you are ready.

## 2. Set your key

```bash
export TALKSIGN_API_KEY="sk-test-…"
```

Never put your key in a browser or mobile app.

## 3. Make your first request

```bash
curl https://api-staging.talksign.co/v1/speech-to-sign/studio \
  -H "Authorization: Bearer $TALKSIGN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "The next train to Leeds leaves from platform 4.",
    "language": "asl",
    "avatar": "amara"
  }'
```

## 4. Get your video

```json
{
  "success": true,
  "data": {
    "videoUrl": "https://api.talksign.co/v1/speech-to-sign/video/req_8f2k1x",
    "video_url": "https://api.talksign.co/v1/speech-to-sign/video/req_8f2k1x",
    "download_url": "https://storage.googleapis.com/…/req_8f2k1x.mp4?…",
    "expires_at": "2026-11-22T13:00:00.000Z",
    "requestId": "req_8f2k1x",
    "durationSeconds": 4.2,
    "avatar": "amara",
    "language": "asl"
  },
  "usage": {
    "type": "speech-to-sign/studio",
    "durationSeconds": 4.2,
    "costUsd": 0.1,
    "walletRemainingUsd": 49.9
  }
}
```

## Live sign clips

### 2. Warm up when your interpreter view opens

The signing engine sleeps when idle; the first call after a quiet period is slower. Call
`/v1/warmup` (free, not billed) when your user opens your interpreter view, so their
first sentence is fast:

```bash
curl -X POST https://api-staging.talksign.co/v1/warmup \
  -H "Authorization: Bearer $TALKSIGN_API_KEY"
# → { "warm": true, "ms": 412 }
```

Fire it and carry on; you do not need to wait for the answer.

### 2b. First realtime call

```bash
curl -X POST https://api-staging.talksign.co/v1/speech-to-sign/realtime \
  -H "Authorization: Bearer $TALKSIGN_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{ "text": "Hello, how are you?", "language": "asl", "avatar": "arthur" }'
```

Response (shortened):

```json
{
  "success": true,
  "data": {
    "language": "asl",
    "avatar": "arthur",
    "text": "Hello, how are you?",
    "glosses": ["HELLO", "HOW", "YOU"],
    "playlist": [
      {
        "gloss": "HELLO",
        "url": "https://…/HELLO.mp4?…",
        "in_url": "https://…/HELLO.in.mp4?…",
        "out_url": "https://…/HELLO.out.mp4?…",
        "active_start_s": 0.6667,
        "active_end_s": 1.5333,
        "in_out_speed": 1.5,
        "alphaUrl": null, "in_alpha_url": null, "out_alpha_url": null,
        "group": null, "letter": null,
        "tier": "TIER_1_EXACT",
        "transition": { "effect": "crossfade", "duration_ms": 150 }
      }
    ],
    "idle": { "url": "https://…/_IDLE.mp4?…", "expires_at": "2026-11-22T18:00:00.000Z" },
    "expires_at": "2026-11-22T18:00:00.000Z",
    "has_missing": false
  },
  "usage": { "type": "speech-to-sign/realtime", "durationSeconds": 2, "costUsd": 0.02, "walletRemainingUsd": 49.98 }
}
```

Call this endpoint **from your server**, never from the browser: the key is a secret.
Send the `data` object (it holds no secrets) to your page.

### 3. Play the clips

Every word starts and ends in the same neutral pose, so words join without a cut if
you play them in this order:

1. `in_url` (neutral pose → start of the sign), at `in_out_speed`
2. `url` from `active_start_s` to `active_end_s` (the sign itself), at normal speed
3. `out_url` (end of the sign → neutral pose), at `in_out_speed`
4. the next word's `in_url`, and so on

If `in_url`/`out_url` are `null`, play `url` from start to end.
Between sentences, loop `idle.url` (the avatar breathing in its rest pose) so the
avatar never freezes or disappears. `idle` can be `null`; then show the last frame.

All URLs stop working at `expires_at` (6 to 12 hours after the call). Do not store
them; ask the API again for a new sentence.

#### JavaScript example (browser)

Two `<video>` elements swapped back to back give gapless playback: one plays while
the next one preloads.

```html
<div id="stage" style="position:relative;width:360px;aspect-ratio:9/16">
  <video id="a" muted playsinline style="position:absolute;inset:0;width:100%"></video>
  <video id="b" muted playsinline style="position:absolute;inset:0;width:100%;visibility:hidden"></video>
</div>
<script type="module">
const players = [document.getElementById('a'), document.getElementById('b')];
let front = 0;

// Flatten the playlist into segments: { src, start, end, rate }.
function segments(playlist) {
  const out = [];
  for (const c of playlist) {
    if (!c.url) continue;                           // missing word (has_missing)
    const rate = c.in_out_speed || 1;
    if (c.in_url && c.out_url) {
      out.push({ src: c.in_url, start: 0, end: null, rate });
      out.push({ src: c.url, start: c.active_start_s ?? 0, end: c.active_end_s ?? null, rate: 1 });
      out.push({ src: c.out_url, start: 0, end: null, rate });
    } else {
      out.push({ src: c.url, start: 0, end: null, rate: 1 });
    }
  }
  return out;
}

function load(video, seg) {
  return new Promise((resolve) => {
    video.src = seg.src;
    video.playbackRate = seg.rate;
    video.onloadedmetadata = () => { video.currentTime = seg.start; resolve(); };
    video.load();
  });
}

function playSegment(video, seg) {
  return new Promise((resolve) => {
    const stop = () => { video.ontimeupdate = null; video.onended = null; video.pause(); resolve(); };
    video.onended = stop;
    video.ontimeupdate = () => { if (seg.end != null && video.currentTime >= seg.end) stop(); };
    video.play();
  });
}

async function playIdle(idle) {
  const v = players[front];
  if (!idle) return;                                // keep the last frame
  v.loop = true; v.src = idle.url; v.playbackRate = 1; await v.play();
}

export async function sign(data) {
  const segs = segments(data.playlist);
  if (!segs.length) return playIdle(data.idle);
  await load(players[1 - front], segs[0]);
  for (let i = 0; i < segs.length; i++) {
    const cur = players[1 - front];
    cur.loop = false;
    cur.style.visibility = 'visible';
    players[front].style.visibility = 'hidden';
    front = 1 - front;
    const next = segs[i + 1] ? load(players[1 - front], segs[i + 1]) : null; // preload
    await playSegment(cur, segs[i]);
    if (next) await next;
  }
  await playIdle(data.idle);
}

// Your server calls /v1/speech-to-sign/realtime and returns `data`.
const data = await (await fetch('/my-backend/sign', { method: 'POST', body: JSON.stringify({ text: 'Hello, how are you?' }) })).json();
sign(data);
</script>
```

Transparent background: when `alphaUrl` (and `in_alpha_url` / `out_alpha_url`) are
present they hold a matte (colour and mask side by side) you can composite in a
`<canvas>` or WebGL shader. They are optional and may be `null`.

Fingerspelling: clips of one spelled word share a `group` and each has a `letter`;
show the word once in captions.

### 4. Go live

1. Accept the Developer API terms in the portal.
2. Top up the wallet (minimum $50) – see "Pricing and billing".
3. Create a live key and swap `sk-test-…` for `sk-live-…`.
4. Keep the `Idempotency-Key` header on every paid POST so retries never charge twice.

Next: "API reference".
