HTTP API Examples

These examples use the HTTP API directly. They are examples, not SDKs.

Transcribe with Python

from __future__ import annotations

import json
import os
import urllib.request
import uuid
from pathlib import Path

base_url = os.environ.get("AUDIOTEXT_BASE_URL", "http://127.0.0.1:8791")
token = os.environ["AUDIOTEXT_API_TOKEN"]
audio_path = Path("clip.webm")
boundary = f"audiotext-{uuid.uuid4().hex}"

parts = [
    f"--{boundary}\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\ncpu-lite\r\n".encode(),
    f"--{boundary}\r\nContent-Disposition: form-data; name=\"language\"\r\n\r\nca\r\n".encode(),
    (
        f"--{boundary}\r\n"
        f"Content-Disposition: form-data; name=\"file\"; filename=\"{audio_path.name}\"\r\n"
        "Content-Type: audio/webm\r\n\r\n"
    ).encode(),
    audio_path.read_bytes(),
    f"\r\n--{boundary}--\r\n".encode(),
]
body = b"".join(parts)

request = urllib.request.Request(
    f"{base_url}/v1/audio/transcriptions",
    data=body,
    headers={
        "Authorization": f"Bearer {token}",
        "Content-Type": f"multipart/form-data; boundary={boundary}",
        "Content-Length": str(len(body)),
    },
    method="POST",
)

with urllib.request.urlopen(request, timeout=300) as response:
    print(json.dumps(json.loads(response.read()), indent=2, ensure_ascii=False))

Transcribe with Node

import { readFile } from "node:fs/promises";

const baseUrl = process.env.AUDIOTEXT_BASE_URL ?? "http://127.0.0.1:8791";
const token = process.env.AUDIOTEXT_API_TOKEN;
const audio = await readFile("clip.webm");
const form = new FormData();

form.set("model", "cpu-lite");
form.set("language", "ca");
form.set("file", new Blob([audio], { type: "audio/webm" }), "clip.webm");

const response = await fetch(`${baseUrl}/v1/audio/transcriptions`, {
  method: "POST",
  headers: { Authorization: `Bearer ${token}` },
  body: form,
});

if (!response.ok) {
  throw new Error(`${response.status} ${await response.text()}`);
}

console.log(await response.json());

Use async jobs

Create the job:

curl -sS "$AUDIOTEXT_BASE_URL/v1/transcription-jobs" \
  -H "Authorization: Bearer $AUDIOTEXT_API_TOKEN" \
  -F model=cpu-lite \
  -F language=ca \
  -F file=@clip.webm

Poll status:

curl -sS "$AUDIOTEXT_BASE_URL/v1/transcription-jobs/$JOB_ID" \
  -H "Authorization: Bearer $AUDIOTEXT_API_TOKEN"

Read only the result:

curl -sS "$AUDIOTEXT_BASE_URL/v1/transcription-jobs/$JOB_ID/result" \
  -H "Authorization: Bearer $AUDIOTEXT_API_TOKEN"

Cancel a queued job:

curl -sS -X POST "$AUDIOTEXT_BASE_URL/v1/transcription-jobs/$JOB_ID/cancel" \
  -H "Authorization: Bearer $AUDIOTEXT_API_TOKEN"