One thing that’s easy to miss with TTS APIs is that the response usually isn’t something you want to handle as normal text. If the endpoint returns the actual audio, you need to save the response as binary data.
For example, with Python:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
with open("speech.mp3", "wb") as audio:
audio.write(response.content)
The "wb" is important here because MP3/WAV data is binary. If you open the file in regular text mode, the output can become corrupted.
With curl, the same idea is usually handled with the -o option:
curl ... -o speech.mp3
If you're getting a file that won't play, I'd first check the response Content-Type and the actual response body. Some APIs return JSON containing an audio URL rather than returning the audio itself, which needs to be handled differently.
One thing that’s easy to miss with TTS APIs is that the response usually isn’t something you want to handle as normal text. If the endpoint returns the actual audio, you need to save the response as binary data.
For example, with Python:
The
"wb"is important here because MP3/WAV data is binary. If you open the file in regular text mode, the output can become corrupted.With
curl, the same idea is usually handled with the-ooption:If you're getting a file that won't play, I'd first check the response Content-Type and the actual response body. Some APIs return JSON containing an audio URL rather than returning the audio itself, which needs to be handled differently.