#How can I get the audio request id when using the python sdk?
1 messages · Page 1 of 1 (latest)
When making a text-to-speech request using the ElevenLabs API, the request-id can be found in the response headers. This ID is essential for referencing previous requests to maintain prosody and consistency in audio generation.
If you're using the Python SDK, ensure you capture the response object after making the API call. You can then access the request-id from the response headers. Here's an example of how you might do this:
import requests
# Define constants
XI_API_KEY = "<your-api-key>"
VOICE_ID = "<voice-id>"
TEXT_TO_SPEAK = "Hello, this is a test."
# Set up the API endpoint and headers
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
headers = {
"Accept": "application/json",
"xi-api-key": XI_API_KEY
}
# Set up the data payload
data = {
"text": TEXT_TO_SPEAK,
"model_id": "eleven_multilingual_v2",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.8
}
}
# Make the POST request
response = requests.post(url, headers=headers, json=data, stream=True)
# Check if the request was successful
if response.ok:
# Retrieve the request ID from the response headers
request_id = response.headers.get("request-id")
print(f"Request ID: {request_id}")
else:
print(f"Error: {response.text}")
Replace <your-api-key> and <voice-id> with your actual API key and voice ID. This code will print the request-id from the response headers, which you can use for subsequent requests.
This is a good example for the API, but what if I am using the python sdk and calling text-to-speech like this:
audio = client.text_to_speech.convert(
text="The first move is what sets everything in motion.",
voice_id="JBFqnCBsd6RMkjVDRZzb",
model_id="eleven_multilingual_v2",
output_format="mp3_44100_128",
)