Create a Live Token
Get a single-use token that opens one KhasbhaSTT LIVE session, for your server, an app or a browser
curl -X POST https://api.voice213.com/api/v1/stt/live/token \
-H "Authorization: Bearer $VOICE213_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"language": "ar",
"dialect": "algerian",
"vocabulary": ["Voice213", "Yalidine"],
"maxSeconds": 600
}'import express from "express";
import Voice213, { APIError } from "@voice213/sdk";
const app = express();
const voice213 = new Voice213(); // reads VOICE213_API_KEY
// The page that opens the socket: other websites can't use the token
const SITE_ORIGIN = "https://shop.example";
app.post("/api/live-token", async (req, res) => {
// Check here that the visitor is one of your signed-in users
try {
const token = await voice213.speechToText.live.createToken({
language: "ar",
dialect: "algerian",
maxSeconds: 600,
origin: SITE_ORIGIN,
});
res.json({ url: token.url }); // the browser opens this socket
} catch (error) {
res.status(error instanceof APIError ? error.status : 502).json({ error: error.message });
}
});
app.listen(8080);from fastapi import FastAPI, HTTPException
from voice213 import APIError, AsyncVoice213
app = FastAPI()
client = AsyncVoice213() # reads VOICE213_API_KEY
SITE_ORIGIN = "https://shop.example" # the page that opens the socket
@app.post("/api/live-token")
async def live_token():
# Check here that the caller is one of your signed-in users
try:
token = await client.speech_to_text.live.create_token(
language="ar", dialect="algerian", max_seconds=600, origin=SITE_ORIGIN
)
except APIError as error:
raise HTTPException(error.status_code, error.message)
return {"url": token.url} # the browser opens this socket{
"success": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI2NmYxYzBhNGUxYjJjM2Q0ZTVmNjA3MTgi…",
"url": "wss://api.voice213.com/ws/stt?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI2NmYxYzBhNGUxYjJjM2Q0ZTVmNjA3MTgi…",
"wsUrl": "wss://api.voice213.com/ws/stt",
"expiresIn": 120,
"model": "khasbha-stt-live",
"language": "ar",
"dialect": "algerian",
"maxSeconds": 600,
"sampleRate": 16000,
"encoding": "pcm_s16le",
"channels": 1,
"pricePerMinute": 5,
"minBillableSeconds": 15,
"currency": "DZD"
}{
"error": "INVALID_VOCABULARY",
"message": "`vocabulary` must be an array of at most 50 words, each 1 to 40 characters long.",
"maxTerms": 50,
"maxTermChars": 40
}{
"error": "INSUFFICIENT_CREDIT",
"message": "Not enough API credit. Top up your balance in the developer workspace.",
"required": 1.25,
"balance": 0.6,
"currency": "DZD"
}{
"error": "SESSION_LIMIT",
"message": "You already have 5 live sessions running.",
"limit": 5
}Creates a single-use token for one KhasbhaSTT LIVE session, and returns the address of the socket to open with it. Call it from your server, with your API key. The token is what you hand to a browser or an app, never the key. See Live Transcription Socket for what happens on the socket, and KhasbhaSTT LIVE for a guide.
- One session, within two minutes. A token opens a single session, and only if it's used within 120 seconds of being created.
- The settings travel in the token. The
language,dialect,vocabulary,maxSecondsandoriginyou send here are fixed for the session. To change them, create a new token. - Tied to your website. A token created with an
originonly opens a socket from a page on that origin, so other websites can't use it. Set it for every token you hand to a browser. - Free, but counted. Creating a token isn't billed: the session is, per second of audio. Each token counts as one request toward your account's requests per minute and per day.
- Checked twice. Your API credit and your running sessions are checked here, and again when the socket opens.
Authorizations
Your API key as a Bearer token: Bearer sk_live_.... You can send it in an x-api-key header instead. See Authentication.
Body
application/jsonEvery field is optional: an empty body creates a session for Algerian darija that can last up to an hour, as far as your API credit covers.
arThe language people will speak: ar (Arabic, darija included), fr (French) or en (English), in any capitalization. Any other value gets 400 INVALID_LANGUAGE. See Languages and dialects.
algerianThe Arabic dialect the transcription is tuned for: algerian, tunisian or libyan, in any capitalization. It applies when language is ar, and is null in the answer otherwise. Any other value gets 400 INVALID_DIALECT.
Words the transcription should recognize, such as your brand, your products, people and places: up to 50 terms, each 1 to 40 characters long once spaces are trimmed. Duplicates, in any capitalization, are dropped, and the first spelling is kept. Anything else, such as a term that isn't a string, gets 400 INVALID_VOCABULARY.
3600The longest the session may last, in seconds: a whole number from 15 to 3,600. The session gets an ENDING_SOON warning 30 seconds before, then ends with the reason max_duration. Use it to cap what one session can spend: 600 seconds cost 50 DA at most. Any other value gets 400 INVALID_MAX_SECONDS.
The origin of the web page that will open the socket, written scheme://host or scheme://host:port, such as https://shop.example or http://localhost:8080: http or https, and no path (a trailing slash is fine). The socket must then be opened with exactly that Origin header, which browsers send for the page that opens it; any other origin, or none, is refused with 401 during the handshake. https://www.shop.example and https://shop.example are two different origins. Leave it out for a socket opened by your server or an app. Anything else gets 400 INVALID_ORIGIN.
Response
application/jsontrue.
The token. It opens one session, within expiresIn seconds.
The socket to open, with the token already in it: wss://api.voice213.com/ws/stt?token=….
The socket without the token, wss://api.voice213.com/ws/stt, if you'd rather add ?token= yourself.
Seconds left to open the socket with the token: 120.
khasbha-stt-live.
The session's language: ar, fr or en.
The dialect the session uses for Arabic, such as algerian, or null when language isn't ar.
How long the session could last if it started now: your maxSeconds, and no longer than your available API credit covers. It's checked again when the socket opens, and the ready message has the final figure.
The rate of the audio to send: 16000 Hz.
The audio format to send: pcm_s16le, 16-bit signed little-endian PCM.
1: mono.
The session's price, in dinars per minute of audio.
The fewest seconds a session that receives audio is billed: 15.
DZD.
Errors
| Status | Error | When |
|---|---|---|
400 | INVALID_LANGUAGE | language isn't ar, fr or en. The body lists the languages. |
400 | INVALID_DIALECT | dialect isn't algerian, tunisian or libyan. The body lists the dialects. |
400 | INVALID_VOCABULARY | vocabulary isn't a list of up to 50 terms of 1 to 40 characters. The body has maxTerms and maxTermChars. |
400 | INVALID_MAX_SECONDS | maxSeconds isn't a whole number from 15 to 3,600. The body has min and max. |
400 | INVALID_ORIGIN | origin isn't an http or https origin without a path, such as https://shop.example. |
401 | Missing API key. Pass it as 'Authorization: Bearer sk_live_...' | The request has no key. |
401 | Invalid or inactive API key | The key is wrong, switched off or deleted. |
402 | INSUFFICIENT_CREDIT | Your API credit, less what your running requests and sessions have set aside, can't cover the shortest session, 15 seconds (1.25 DA). The body has required and balance. |
403 | Account deactivated | The account that owns the key is deactivated or suspended. |
403 | API_ACCESS_BLOCKED (in code) | API access is switched off for the account. |
429 | SESSION_LIMIT | Your account already has 5 live sessions running. Retry when one of them ends. The body has the limit. |
429 | Rate limit exceeded: RPM limit exceeded or Rate limit exceeded: Daily limit exceeded | Your account's requests per minute or per day, all its keys together. |
500 | INTERNAL | The token couldn't be created. Retry in a moment. |
503 | TOOL_DISABLED (in code) | Speech to text through the API is paused (tool is stt_api), or every paid request of the API is (public_api). Retry later. Sessions already running carry on. |
503 | MAINTENANCE (in code) | The API is down for maintenance. Retry later. |
What happens once the socket is open, such as a session refused at that point, is in the socket's close codes and errors.
Was this page helpful?