## Async TTS with retrieval

### Python

```
import requests
import json

# Step 1: Initiate async TTS
url = "https://api.upliftai.org/v1/synthesis/text-to-speech-async"

payload = json.dumps({
  "voiceId": "v_meklc281",
  "text": "سلام، یہ پاکستان کی تاریخ کے بارے میں ہے۔",
  "outputFormat": "MP3_22050_128"
})
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer YOUR_API_KEY'
}

response = requests.post(url, headers=headers, data=payload)
result = response.json()

# Step 2: Retrieve audio when ready
media_id = result['mediaId']
token = result['token']

audio_url = f"https://api.upliftai.org/v1/synthesis/stream-audio/{media_id}?token={token}"

# Get the audio
audio_response = requests.get(audio_url)

# Save to file
with open('output.mp3', 'wb') as f:
    f.write(audio_response.content)
```

```javascript
// Server-side: Initiate TTS
async function initiateTTS() {
  const response = await fetch('https://api.upliftai.org/v1/synthesis/text-to-speech-async', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer YOUR_API_KEY'
    },
    body: JSON.stringify({
      voiceId: "v_meklc281",
      text: "سلام، یہ پاکستان کی تاریخ کے بارے میں ہے۔",
      outputFormat: "MP3_22050_128"
    })
  });

const { mediaId, token } = await response.json();

// Send URL to client or webhook
  const audioUrl = `https://api.upliftai.org/v1/synthesis/stream-audio/${mediaId}?token=${token}`;

// Client can now fetch audio directly
  return audioUrl;
}
```

```bash
curl --request POST \
  --url https://api.upliftai.org/v1/synthesis/text-to-speech-async \
  --header 'Authorization: <api-key>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "text": "سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔",
  "voiceId": "v_meklc281",
  "phraseReplacementConfigId": "<string>"
}
'
```

```php
<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.upliftai.org/v1/synthesis/text-to-speech-async",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode([
    'text' => 'سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔',
    'voiceId' => 'v_meklc281',
    'phraseReplacementConfigId' => '<string>'
  ]),
  CURLOPT_HTTPHEADER => [
    "Authorization: <api-key>",
    "Content-Type: application/json"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

url := "https://api.upliftai.org/v1/synthesis/text-to-speech-async"

payload := strings.NewReader("{\n  \"text\": \"سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔\",\n  \"voiceId\": \"v_meklc281\",\n  \"phraseReplacementConfigId\": \"<string>\"\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Authorization", "<api-key>")
	req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))
}
```

```java
HttpResponse<String> response = Unirest.post("https://api.upliftai.org/v1/synthesis/text-to-speech-async")
  .header("Authorization", "<api-key>")
  .header("Content-Type", "application/json")
  .body("{\n  \"text\": \"سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔\",\n  \"voiceId\": \"v_meklc281\",\n  \"phraseReplacementConfigId\": \"<string>\"\n}")
  .asString();
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.upliftai.org/v1/synthesis/text-to-speech-async")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"text\": \"سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔\",\n  \"voiceId\": \"v_meklc281\",\n  \"phraseReplacementConfigId\": \"<string>\"\n}"

response = http.request(request)
puts response.read_body
```

### Response Codes

- 200
- 400
- 429

### Sample Responses

```json
{
  "mediaId": "media_abc123xyz",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
```

```json
{
  "message": "Invalid request parameters"
}
```

```json
{
  "message": "Rate limit exceeded, please try again later"
}
```

### Authorizations

**Authorization**: string, header, required
- API key with format "Bearer sk_api_..."

### Request Body

**Content-Type**: application/json

#### Required Fields

- **text**: string, required, Maximum string length: `2500`
    - Example: "سلام، آپ اِس وقت اوریٹر کی آواز سن رہے ہیں۔"
- **outputFormat**: enum<string>, required
    - Available options: `PCM_22050_16`, `WAV_22050_16`, `WAV_22050_32`, `MP3_22050_32`, `MP3_22050_64`, `MP3_22050_128`, `OGG_22050_16`, `ULAW_8000_8`
- **voiceId**: string
    - Example: "v_meklc281"
- **phraseReplacementConfigId**: string, optional
    
### Sample Response

```json
{
  "mediaId": "media_abc123xyz",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
```
