> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gnani.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Voice Cloning quickstart

> Clone a voice from a short recording and use it to speak new text in a few minutes.

Voice cloning takes two requests. First you upload a short recording of the voice and get back a speaker embedding. Then you send text with that embedding and get audio in the cloned voice. The example does both in one run.

You need a recording of 5 to 30 seconds of one person speaking clearly, with little background noise. The examples call it `reference.wav`.

> [!WARNING]
>
> Only clone a voice when the speaker has agreed to it. A speaker embedding represents a real person's voice: store it as securely as you would the recording, and do not share it.

**1. Create an API key**

Sign in to the [Gnani API dashboard](https://app.gnani.ai/voice) and generate an API key.

**2. Set your key**

The examples read your key from the `GNANI_API_KEY` environment variable, so it never appears in your code. Set it in the terminal you will run the examples from:

```bash macOS / Linux theme={null}
export GNANI_API_KEY="your-api-key"
```

```powershell Windows (PowerShell) theme={null}
$env:GNANI_API_KEY = "your-api-key"
```

```bash .env theme={null}
GNANI_API_KEY=your-api-key
```

If you use a `.env` file, load it with a library such as `python-dotenv` or `dotenv`, and keep the file out of version control.

**3. Install**

```bash cURL theme={null}
# The cURL example uses jq to pass the embedding from the first request to the second.
jq --version
```

```bash Python theme={null}
pip install requests
```

```bash Node.js theme={null}
# Nothing to install. Node.js 18 or later includes fetch and FormData.
node --version
```

If `jq` is missing, install it from [jqlang.org](https://jqlang.org/download/).

**4. Run**

```bash cURL theme={null}
# 1. Create the speaker embedding
curl --request POST "https://api.vachana.ai/api/v1/tts/voice-clone/embeddings" \
  --header "X-API-Key-ID: $GNANI_API_KEY" \
  --form audio_file=@reference.wav |
jq '.data.voice_clone_embedding' > speaker_embedding.json

# 2. Speak new text in the cloned voice
jq '{
  text: "नमस्ते, आप कैसे हैं?",
  model: "vachana-vc-v1",
  audio_config: {sample_rate: 44100, encoding: "linear_pcm", container: "wav"},
  speaker_embedding: .
}' speaker_embedding.json |
curl --request POST "https://api.vachana.ai/api/v1/tts/inference" \
  --header "Content-Type: application/json" \
  --header "X-API-Key-ID: $GNANI_API_KEY" \
  --data @- \
  --output cloned.wav
```

```python Python theme={null}
# quickstart.py
import os
import requests

HEADERS = {"X-API-Key-ID": os.environ["GNANI_API_KEY"]}

# 1. Create the speaker embedding
with open("reference.wav", "rb") as f:
    r = requests.post(
        "https://api.vachana.ai/api/v1/tts/voice-clone/embeddings",
        headers=HEADERS,
        files={"audio_file": ("reference.wav", f, "audio/wav")},
        timeout=60,
    )
r.raise_for_status()
speaker_embedding = r.json()["data"]["voice_clone_embedding"]

# 2. Speak new text in the cloned voice
r = requests.post(
    "https://api.vachana.ai/api/v1/tts/inference",
    headers=HEADERS,
    json={
        "text": "नमस्ते, आप कैसे हैं?",
        "model": "vachana-vc-v1",
        "audio_config": {
            "sample_rate": 44100,
            "encoding": "linear_pcm",
            "container": "wav",
        },
        "speaker_embedding": speaker_embedding,
    },
    timeout=60,
)

if r.ok:
    with open("cloned.wav", "wb") as f:
        f.write(r.content)
    print("Saved cloned.wav")
else:
    print(r.status_code, r.text)
```

```javascript Node.js theme={null}
// quickstart.mjs
import { readFileSync, writeFileSync } from "node:fs";

const headers = { "X-API-Key-ID": process.env.GNANI_API_KEY };

// 1. Create the speaker embedding
const form = new FormData();
form.append("audio_file", new Blob([readFileSync("reference.wav")], { type: "audio/wav" }), "reference.wav");

const embeddingResponse = await fetch("https://api.vachana.ai/api/v1/tts/voice-clone/embeddings", {
  method: "POST",
  headers,
  body: form,
});
if (!embeddingResponse.ok) {
  throw new Error(`${embeddingResponse.status} ${await embeddingResponse.text()}`);
}
const { data } = await embeddingResponse.json();
const speakerEmbedding = data.voice_clone_embedding; // { embedding, shape, dtype }

// 2. Speak new text in the cloned voice
const response = await fetch("https://api.vachana.ai/api/v1/tts/inference", {
  method: "POST",
  headers: { ...headers, "Content-Type": "application/json" },
  body: JSON.stringify({
    text: "नमस्ते, आप कैसे हैं?",
    model: "vachana-vc-v1",
    audio_config: { sample_rate: 44100, encoding: "linear_pcm", container: "wav" },
    speaker_embedding: speakerEmbedding,
  }),
});

if (response.ok) {
  writeFileSync("cloned.wav", Buffer.from(await response.arrayBuffer()));
  console.log("Saved cloned.wav");
} else {
  console.log(response.status, await response.text());
}
```

Run the Python example with `python quickstart.py` and the Node.js example with `node quickstart.mjs`.

> [!NOTE]
>
> There is no Python SDK tab: the Python SDK can use a speaker embedding but cannot create one. See [Voice cloning in the Python SDK](https://docs.gnani.ai/python-sdk/text-to-speech.md#voice-cloning).

**5. Check the output**

The first request returns `200` and a speaker embedding in `data.voice_clone_embedding`:

```json theme={null}
{
  "success": true,
  "message": "Voice embeddings generated successfully",
  "data": {
    "voice_clone_embedding": {
      "embedding": "<embedding-string>",
      "shape": [1, 768],
      "dtype": "torch.bfloat16"
    }
  }
}
```

The second request returns `200` with WAV audio, saved as `cloned.wav`. Play it: you should hear "नमस्ते, आप कैसे हैं?" ("Hello, how are you?") in the voice from your recording.

The cURL example saves it as `speaker_embedding.json`. Keep the embedding and reuse it for later requests. You only need to create it once per voice.

## If it didn't work

Voice Cloning errors return a JSON body with a `message` that says what is wrong, instead of an embedding or audio.

| Status | Error | Fix |
| - | - | - |
| `401` | `MISSING_API_KEY` | `GNANI_API_KEY` is not set in this terminal. Set it again (step 2) in the same terminal you run the code from. |
| `401` | `INVALID_API_KEY` | The key is wrong or was revoked. Copy it again from the dashboard. |
| `403` | `SCOPE_NOT_ALLOWED` | Your key is not allowed to use Voice Cloning. Use a key that has access to it. |
| `429` | `RATE_LIMITED` | Your account allows fewer requests at a time than you sent. Wait for the earlier request to finish, then retry. |
| `400` | `Audio file is empty` | `reference.wav` is empty or the path is wrong. Check the file. |
| `500` | Server error on the second request | The embedding may be incomplete or changed. Pass `data.voice_clone_embedding` from the first response unchanged as `speaker_embedding`. |

Every error is listed in the [Error Reference](https://docs.gnani.ai/api/introduction/errors.md#voice-cloning).

## Next

> [card] [Voice Clone Embeddings](https://docs.gnani.ai/api/VC/voice-clone-embeddings.md)
>
> What makes a good reference recording.

> [card] [Streaming](https://docs.gnani.ai/api/VC/vc-sse.md)
>
> Receive cloned audio as it is generated.

> [card] [Realtime](https://docs.gnani.ai/api/VC/vc-websocket.md)
>
> Lowest latency over a WebSocket.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.