# FasihTTS HD API examples

Download the Node.js or Python example from https://www.fasihtts.com/en/api-docs.
These are server-side examples: never put API keys in browser code or commit them.
They require a paid allowance and an API key from your dashboard.

1. Save your exact Arabic script as UTF-8 `script.txt`.
2. Set `FASIHTTS_API_KEY` privately in your terminal or deployment environment.
3. Generate a unique request ID locally, for example `python3 -c "import uuid; print(uuid.uuid4())"`. Save it together with the exact script, voice and dialect, then set `FASIHTTS_REQUEST_ID` to that saved value **before** submitting. The examples refuse to create a job without it.
4. Run `node fasih-hd.mjs script.txt speech.mp3` (Node 20+) or `python3 fasih-hd.py script.txt speech.mp3`.
5. The default is Modern Standard Arabic. Optional `FASIHTTS_VOICE` and `FASIHTTS_DIALECT` must match an available voice returned by `list-voices`.

Each new request consumes your normal allowance. The API automatically selects available audio seconds or existing character quota; these balances stay separate. For an unchanged retry, set `FASIHTTS_REQUEST_ID` to the saved ID used on the first run. Do not change text/voice while reusing an ID (409 conflict). After a timeout with a known job ID, set `FASIHTTS_JOB_ID` and rerun the same command to poll that job without re-submitting or reading the script. Without a job ID, repeat the identical payload with the original `FASIHTTS_REQUEST_ID`. Never create a fresh key to resolve uncertainty. Unset `FASIHTTS_JOB_ID` only when intentionally submitting a new generation. Download retries are free. The examples do not automatically retry a failed generation.

## n8n / Make recipe

Use generic HTTP request steps; no paid connector is needed:

1. Create a stable unique request ID for this script and save it with your workflow item. Keep the API key in the platform's credential store.
2. POST `https://xrwqtifhqycpxfjzfrip.supabase.co/functions/v1/generate-tts-v2` with headers `x-api-key`, `Content-Type: application/json`, and `Idempotency-Key`. JSON: `{"text":"مرحبًا بكم","voice":"ar-msa-formal-male-2","dialect":"ar-msa"}`.
3. Save `jobId`; wait 2–5 seconds. POST `https://xrwqtifhqycpxfjzfrip.supabase.co/functions/v1/get-long-tts-job` with the same API-key header and JSON `{"id":"YOUR_JOB_ID"}`.
4. While queued/processing, loop through the wait and status steps with a bounded timeout. If failed, stop and notify yourself. Never loop back through the generation step with a new ID.
5. When completed, GET `job.audio_url` as a binary file **without forwarding your API key**. Links expire, so poll the same job for a fresh link while the audio is retained. If `audioExpired` is true or `audio_url` is null, polling cannot restore unavailable audio. Save completed audio promptly.

The website's previous `/v1/tts` API remains available for eligible legacy accounts and previous voices, with synchronous base64 audio and character accounting. HD/private voices return `409 api_upgrade_required` on that endpoint. New HD voices use the asynchronous endpoint above. These examples have syntax and contract validation; executing them against your account consumes allowance.

## Speech to text API

Paid STT accepts your existing FasihTTS `x-api-key` on the server. The API documentation covers the exact upload → quote → start → poll flow at `https://api.fasihtts.com/v1/stt/account/`. Upload raw MP3 bytes (not multipart or base64 JSON), preserve the UUID request key and quote, and poll the saved job ID. Paid uploads are up to 2 minutes / 5 MiB and use your shared audio-minute balance. STT returns text, not generated audio; these TTS scripts are not STT clients.

Free STT samples remain a signed-in web feature at https://www.fasihtts.com/en/transcribe: up to 30 seconds / 5 MiB from the same five-sample TTS/STT allowance. API keys do not authorize free-sample uploads or jobs. No provider key is needed.
