# A Hindi or Hinglish agent

> Pick the language and a voice recorded in it, and let the agent follow the caller between Hindi and English.

An agent that speaks Hindi the way your callers do, in a voice recorded for it, and that follows a caller who drifts into English and back. It is three requests: read what is on offer, pick a voice, create the agent.

## Before you start

- An agent, and its ID. The [Quickstart](https://www.cracklehq.com/docs/quickstart) makes one.
- A workspace API key in the `CRACKLE_API_KEY` environment variable. See [Authentication](https://www.cracklehq.com/docs/authentication).

### 1. See which languages there are

The catalog lists every language an agent can speak. Each has an `id`, which is what you will set on the agent.

Request:

cURL:

```bash
curl "https://goblins-call-api.onrender.com/v1/catalog" \
  -H "Authorization: Bearer $CRACKLE_API_KEY"
```

Node.js:

```js
const response = await fetch('https://goblins-call-api.onrender.com/v1/catalog', {
  headers: {
    Authorization: `Bearer ${process.env.CRACKLE_API_KEY}`,
  },
});
if (!response.ok) throw new Error(await response.text());
const { catalog } = await response.json();
```

Python:

```python
import os
import requests

response = requests.get(
    "https://goblins-call-api.onrender.com/v1/catalog",
    headers={"Authorization": f"Bearer {os.environ['CRACKLE_API_KEY']}"},
    timeout=30,
)
response.raise_for_status()
catalog = response.json()["catalog"]
```

catalog.languages (shortened):

```json
[
  { "id": "hi", "label": "Hindi", "iso": "hi", "native": "हिन्दी" },
  { "id": "hinglish", "label": "Hinglish", "iso": "hi", "native": "Hindi + English" },
  { "id": "en", "label": "English", "iso": "en" }
]
```

**Hindi** is for callers who speak it throughout. **Hinglish** is Hindi with English words mixed in, the way it is commonly spoken. If your callers move between the two in one sentence, choose Hinglish.

### 2. Pick a voice recorded in Hindi

Ask the voice library for Hindi voices. Hinglish is spoken by Hindi voices, so either language name finds them.

Request:

cURL:

```bash
curl "https://goblins-call-api.onrender.com/v1/providers/voices?language=hinglish&gender=feminine&limit=10" \
  -H "Authorization: Bearer $CRACKLE_API_KEY"
```

Node.js:

```js
const response = await fetch('https://goblins-call-api.onrender.com/v1/providers/voices?language=hinglish&gender=feminine&limit=10', {
  headers: {
    Authorization: `Bearer ${process.env.CRACKLE_API_KEY}`,
  },
});
if (!response.ok) throw new Error(await response.text());
const { voices } = await response.json();
```

Python:

```python
import os
import requests

response = requests.get(
    "https://goblins-call-api.onrender.com/v1/providers/voices?language=hinglish&gender=feminine&limit=10",
    headers={"Authorization": f"Bearer {os.environ['CRACKLE_API_KEY']}"},
    timeout=30,
)
response.raise_for_status()
voices = response.json()["voices"]
```

Each voice with `preview: true` has a sample. Listen before you choose; a voice is most of how an agent comes across.

Request:

cURL:

```bash
curl "https://goblins-call-api.onrender.com/v1/providers/voices/VOICE_ID/preview" \
  -H "Authorization: Bearer $CRACKLE_API_KEY" \
  --output sample.wav
```

Node.js:

```js
const response = await fetch('https://goblins-call-api.onrender.com/v1/providers/voices/VOICE_ID/preview', {
  headers: {
    Authorization: `Bearer ${process.env.CRACKLE_API_KEY}`,
  },
});
if (!response.ok) throw new Error(await response.text());
const audio = Buffer.from(await response.arrayBuffer());
```

Python:

```python
import os
import requests

response = requests.get(
    "https://goblins-call-api.onrender.com/v1/providers/voices/VOICE_ID/preview",
    headers={"Authorization": f"Bearer {os.environ['CRACKLE_API_KEY']}"},
    timeout=30,
)
response.raise_for_status()
audio = response.content
```

### 3. Create the agent

Set the language and the voice, and say in the instructions how the agent should speak. `changeLanguage` lets it move between Hindi and English when the caller does.

Request:

cURL:

```bash
curl -X POST "https://goblins-call-api.onrender.com/v1/agents" \
  -H "Authorization: Bearer $CRACKLE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Tara",
  "systemInstruction": "You are Tara, and you answer calls for Sunrise Dental in Jaipur.\nSpeak Hinglish: everyday Hindi with the English words people really use, such as appointment, doctor and confirm.\nBe warm and brief. One or two short sentences per reply.\nIf the caller switches to English, switch with them, and switch back when they do.",
  "greetingInstruction": "Greet the caller warmly in Hinglish and ask how you can help.",
  "voice": {
    "language": "hinglish",
    "speaker": "VOICE_ID"
  },
  "systemTools": {
    "changeLanguage": true,
    "endCall": true
  }
}'
```

Node.js:

```js
const response = await fetch('https://goblins-call-api.onrender.com/v1/agents', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.CRACKLE_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'Tara',
    systemInstruction: `You are Tara, and you answer calls for Sunrise Dental in Jaipur.
Speak Hinglish: everyday Hindi with the English words people really use, such as appointment, doctor and confirm.
Be warm and brief. One or two short sentences per reply.
If the caller switches to English, switch with them, and switch back when they do.`,
    greetingInstruction: 'Greet the caller warmly in Hinglish and ask how you can help.',
    voice: {
      language: 'hinglish',
      speaker: 'VOICE_ID',
    },
    systemTools: {
      changeLanguage: true,
      endCall: true,
    },
  }),
});
if (!response.ok) throw new Error(await response.text());
const { agent } = await response.json();
```

Python:

```python
import os
import requests

response = requests.post(
    "https://goblins-call-api.onrender.com/v1/agents",
    headers={"Authorization": f"Bearer {os.environ['CRACKLE_API_KEY']}"},
    json={
        "name": "Tara",
        "systemInstruction": "You are Tara, and you answer calls for Sunrise Dental in Jaipur.\nSpeak Hinglish: everyday Hindi with the English words people really use, such as appointment, doctor and confirm.\nBe warm and brief. One or two short sentences per reply.\nIf the caller switches to English, switch with them, and switch back when they do.",
        "greetingInstruction": "Greet the caller warmly in Hinglish and ask how you can help.",
        "voice": {
            "language": "hinglish",
            "speaker": "VOICE_ID",
        },
        "systemTools": {
            "changeLanguage": True,
            "endCall": True,
        },
    },
    timeout=30,
)
response.raise_for_status()
agent = response.json()["agent"]
```

## Try it

Press **Test agent** in the studio and talk to it as your callers would. Switch to English for a sentence partway through and it should answer in English, then follow you back to Hindi.

## Things to know

- You can write the instructions in English, as here, or in Hindi. What matters is that they say which language to speak. Crackle adds writing rules for the voice on top, such as which script to write in and how to write numbers so they are read out properly.
- Write names, addresses and product names in the instructions exactly as they should be said.
- For a language other than Hindi or English, set the agent’s `stt.language` to that language’s `iso` code as well as `voice.language`, so the agent listens for the language it speaks. The studio does both when you pick a language there.
- `changeLanguage` moves between Hindi and English only.

Source: https://www.cracklehq.com/docs/cookbooks/hindi-and-hinglish
