API
Text-to-Speech
Convert text to speech audio.
The response body is the raw audio file (binary), not JSON.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | TTS model, e.g. tts-1, tts-1-hd. |
input | string | Yes | Text to synthesize (max 4096 chars). |
voice | string | Yes | alloy, echo, fable, onyx, nova, or shimmer. |
response_format | string | No | mp3 (default), opus, aac, flac, wav, pcm. |
speed | number | No | 0.25–4.0. Default: 1.0. |
Examples
curl -X POST https://silkdock.ai/v1/audio/speech \-H "Authorization: Bearer $SILKDOCK_API_KEY" \-H "Content-Type: application/json" \-d '{ "model": "tts-1", "input": "Hello, world!", "voice": "alloy"}' \--output speech.mp3curl -X POST https://silkdock.ai/v1/audio/speech ^-H "Authorization: Bearer %SILKDOCK_API_KEY%" ^-H "Content-Type: application/json" ^-d "{"model":"tts-1","action":"tts","input":"Hello, world!","voice":"alloy"}" ^--output speech.mp3http POST https://silkdock.ai/v1/audio/speech \Authorization:"Bearer $SILKDOCK_API_KEY" \model=tts-1 \input="Hello, world!" \voice=alloy > speech.mp3wget --method=POST \--header="Authorization: Bearer $SILKDOCK_API_KEY" \--header="Content-Type: application/json" \-O speech.mp3 \https://silkdock.ai/v1/audio/speechInvoke-RestMethod `-Method POST `-Uri "https://silkdock.ai/v1/audio/speech" `-Headers @{ Authorization = "Bearer $env:SILKDOCK_API_KEY" } `-ContentType "application/json" `-OutFile speech.mp3const { OpenAI } = require("openai");const fs = require("fs");const path = require("path");const client = new OpenAI({apiKey: process.env.SILKDOCK_API_KEY,baseURL: "https://silkdock.ai/v1",});const mp3 = await client.audio.speech.create({model: "tts-1",voice: "alloy",input: "Hello, welcome to the text-to-speech service.",});const buffer = Buffer.from(await mp3.arrayBuffer());fs.writeFileSync(path.resolve("./output.mp3"), buffer);console.log("Saved to output.mp3");import { writeFileSync } from "fs";const res = await fetch("https://silkdock.ai/v1/audio/speech", {method: "POST",headers: { "Authorization": `Bearer ${process.env.SILKDOCK_API_KEY}`, "Content-Type": "application/json",},body: JSON.stringify({ model: "tts-1", action: "tts", input: "Hello, world!", voice: "alloy",}),});writeFileSync("speech.mp3", Buffer.from(await res.arrayBuffer()));console.log("Saved to speech.mp3");import axios from "axios";import { writeFileSync } from "fs";const res = await axios.post("https://silkdock.ai/v1/audio/speech",{ model: "tts-1", action: "tts", input: "Hello, world!", voice: "alloy",},{ headers: { Authorization: `Bearer ${process.env.SILKDOCK_API_KEY}`, }, responseType: "arraybuffer",});writeFileSync("speech.mp3", Buffer.from(res.data));console.log("Saved to speech.mp3");// jQuery does not handle binary responses well.// Use XMLHttpRequest directly for binary data:const xhr = new XMLHttpRequest();xhr.open("POST", "https://silkdock.ai/v1/audio/speech");xhr.setRequestHeader("Authorization", `Bearer ${SILKDOCK_API_KEY}`);xhr.setRequestHeader("Content-Type", "application/json");xhr.responseType = "blob";xhr.onload = function () {const url = URL.createObjectURL(xhr.response);const a = document.createElement("a");a.href = url;a.download = "speech.mp3";a.click();URL.revokeObjectURL(url);};xhr.send(JSON.stringify({model: "tts-1",action: "tts",input: "Hello, world!",voice: "alloy",}));import { writeFileSync } from "fs";const xhr = new XMLHttpRequest();xhr.open("POST", "https://silkdock.ai/v1/audio/speech");xhr.setRequestHeader("Authorization", `Bearer ${process.env.SILKDOCK_API_KEY}`);xhr.setRequestHeader("Content-Type", "application/json");xhr.responseType = "arraybuffer";xhr.onload = function () {writeFileSync("speech.mp3", Buffer.from(xhr.response));console.log("Saved to speech.mp3");};xhr.send(JSON.stringify({model: "tts-1",action: "tts",input: "Hello, world!",voice: "alloy",}));const request = require("request");const fs = require("fs");request.post({ url: "https://silkdock.ai/v1/audio/speech", headers: { Authorization: `Bearer ${process.env.SILKDOCK_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "tts-1", action: "tts", input: "Hello, world!", voice: "alloy", }), encoding: null,},(err, res, body) => { if (err) throw err; fs.writeFileSync("speech.mp3", body); console.log("Saved to speech.mp3");});const unirest = require("unirest");const fs = require("fs");unirest.post("https://silkdock.ai/v1/audio/speech").headers({ Authorization: `Bearer ${process.env.SILKDOCK_API_KEY}`, "Content-Type": "application/json",}).send(JSON.stringify({ model: "tts-1", action: "tts", input: "Hello, world!", voice: "alloy",})).buffer(true).then((res) => { fs.writeFileSync("speech.mp3", res.raw_body); console.log("Saved to speech.mp3");});import OpenAI from "openai";import { writeFileSync } from "fs";import path from "path";const client = new OpenAI({apiKey: process.env.SILKDOCK_API_KEY,baseURL: "https://silkdock.ai/v1",});const mp3 = await client.audio.speech.create({model: "tts-1",voice: "alloy",input: "Hello, welcome to the text-to-speech service.",});writeFileSync(path.resolve("./output.mp3"), Buffer.from(await mp3.arrayBuffer()));console.log("Saved to output.mp3");import { writeFileSync } from "fs";const res = await fetch("https://silkdock.ai/v1/audio/speech", {method: "POST",headers: { "Authorization": `Bearer ${process.env.SILKDOCK_API_KEY}`, "Content-Type": "application/json",},body: JSON.stringify({ model: "tts-1", action: "tts", input: "Hello, welcome to the text-to-speech service.", voice: "alloy", response_format: "mp3",}),});writeFileSync("output.mp3", Buffer.from(await res.arrayBuffer()));console.log("Saved to output.mp3");import requests, osres = requests.post( "https://silkdock.ai/v1/audio/speech", headers={"Authorization": f"Bearer {os.getenv('SILKDOCK_API_KEY')}"}, json={ "model": "tts-1", "input": "Hello, welcome to the text-to-speech service.", "voice": "alloy", "response_format": "mp3", },)with open("output.mp3", "wb") as f: f.write(res.content)print("Saved to output.mp3")import osfrom pathlib import Pathfrom openai import OpenAIclient = OpenAI( api_key=os.getenv("SILKDOCK_API_KEY"), base_url="https://silkdock.ai/v1")response = client.audio.speech.create( model="tts-1", voice="alloy", input="Hello, welcome to the text-to-speech service.")response.stream_to_file(Path("output.mp3"))print("Saved to output.mp3")#include <stdio.h>#include <stdlib.h>#include <string.h>#include <curl/curl.h>struct Buffer { unsigned char *data; size_t size;};static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *userdata) { size_t total = size * nmemb; struct Buffer *buf = (struct Buffer *)userdata; unsigned char *tmp = realloc(buf->data, buf->size + total); if (!tmp) return 0; buf->data = tmp; memcpy(buf->data + buf->size, ptr, total); buf->size += total; return total;}int main(void) { const char *api_key = getenv("SILKDOCK_API_KEY"); char auth_header[256]; snprintf(auth_header, sizeof(auth_header), "Authorization: Bearer %s", api_key); const char *body = "{"model":"tts-1","action":"tts"," ""input":"Hello, world!","voice":"alloy"}"; struct Buffer buf = {NULL, 0}; struct curl_slist *headers = NULL; headers = curl_slist_append(headers, auth_header); headers = curl_slist_append(headers, "Content-Type: application/json"); CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, "https://silkdock.ai/v1/audio/speech"); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf); curl_easy_perform(curl); curl_easy_cleanup(curl); curl_slist_free_all(headers); FILE *fp = fopen("speech.mp3", "wb"); fwrite(buf.data, 1, buf.size, fp); fclose(fp); free(buf.data); printf("Saved to speech.mp3\n"); return 0;}#import <Foundation/Foundation.h>int main(int argc, const char *argv[]) { @autoreleasepool { NSString *apiKey = [NSProcessInfo processInfo].environment[@"SILKDOCK_API_KEY"]; NSURL *url = [NSURL URLWithString:@"https://silkdock.ai/v1/audio/speech"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; [request setValue:[NSString stringWithFormat:@"Bearer %@", apiKey] forHTTPHeaderField:@"Authorization"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *bodyDict = @{ @"model": @"tts-1", @"input": @"Hello, world!", @"voice": @"alloy" }; request.HTTPBody = [NSJSONSerialization dataWithJSONObject:bodyDict options:0 error:nil]; dispatch_semaphore_t sem = dispatch_semaphore_create(0); NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"speech.mp3"]; [data writeToFile:path atomically:YES]; NSLog(@"Saved to %@", path); dispatch_semaphore_signal(sem); }] resume]; dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); } return 0;}import com.openai.client.OpenAIClient;import com.openai.client.okhttp.OpenAIOkHttpClient;import com.openai.models.*;import java.nio.file.*;OpenAIClient client = OpenAIOkHttpClient.builder() .apiKey(System.getenv("SILKDOCK_API_KEY")) .baseURL("https://silkdock.ai/v1") .build();SpeechCreateResponse response = client.audio().speech().create( SpeechCreateParams.builder() .model(SpeechModel.TTS_1) .input("Hello, welcome to the text-to-speech service.") .voice(SpeechCreateParams.Voice.ALLOY) .build());Files.write(Path.of("output.mp3"), response.body().bytes());System.out.println("Saved to output.mp3");import java.net.http.*;import java.net.URI;import java.nio.file.*;var req = HttpRequest.newBuilder() .uri(URI.create("https://silkdock.ai/v1/audio/speech")) .header("Authorization", "Bearer " + System.getenv("SILKDOCK_API_KEY")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString( "input":"Hello, welcome to the text-to-speech service.", "voice":"alloy","response_format":"mp3"}""")) .build();var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofByteArray());Files.write(Path.of("output.mp3"), res.body());System.out.println("Saved to output.mp3");import okhttp3.*;import java.io.*;import java.nio.file.*;OkHttpClient client = new OkHttpClient();String json = "{"model":"tts-1","action":"tts"," + ""input":"Hello, world!","voice":"alloy"}";Request request = new Request.Builder() .url("https://silkdock.ai/v1/audio/speech") .addHeader("Authorization", "Bearer " + System.getenv("SILKDOCK_API_KEY")) .post(RequestBody.create(json, MediaType.get("application/json"))) .build();try (Response response = client.newCall(request).execute()) { byte[] bytes = response.body().bytes(); Files.write(Path.of("speech.mp3"), bytes); System.out.println("Saved to speech.mp3");}import kong.unirest.Unirest;import java.nio.file.Files;import java.nio.file.Path;byte[] bytes = Unirest.post("https://silkdock.ai/v1/audio/speech") .header("Authorization", "Bearer " + System.getenv("SILKDOCK_API_KEY")) .header("Content-Type", "application/json") .body("{"model":"tts-1","action":"tts"," + ""input":"Hello, world!","voice":"alloy"}") .asBytes() .getBody();Files.write(Path.of("speech.mp3"), bytes);System.out.println("Saved to speech.mp3");package mainimport ( "context" "io" "os" "github.com/openai/openai-go" "github.com/openai/openai-go/option")func main() { client := openai.NewClient( option.WithAPIKey(os.Getenv("SILKDOCK_API_KEY")), option.WithBaseURL("https://silkdock.ai/v1"), ) resp, _ := client.Audio.Speech.New(context.Background(), openai.AudioSpeechNewParams{ Model: openai.F(openai.SpeechModelTTS1), Input: openai.F("Hello, welcome to the text-to-speech service."), Voice: openai.F(openai.AudioSpeechNewParamsVoiceAlloy), ResponseFormat: openai.F(openai.AudioSpeechNewParamsResponseFormatMp3), }, ) defer resp.Body.Close() data, _ := io.ReadAll(resp.Body) os.WriteFile("output.mp3", data, 0644) println("Saved to output.mp3")}package mainimport ( "bytes" "encoding/json" "net/http" "os")func main() { body, _ := json.Marshal(map[string]any{ "model": "tts-1", "input": "Hello, welcome to the text-to-speech service.", "voice": "alloy", "response_format": "mp3", }) req, _ := http.NewRequest("POST", "https://silkdock.ai/v1/audio/speech", bytes.NewReader(body)) req.Header.Set("Authorization", "Bearer "+os.Getenv("SILKDOCK_API_KEY")) req.Header.Set("Content-Type", "application/json") resp, _ := http.DefaultClient.Do(req) defer resp.Body.Close() buf := new(bytes.Buffer) buf.ReadFrom(resp.Body) os.WriteFile("output.mp3", buf.Bytes(), 0644) println("Saved to output.mp3")}<?php$ch = curl_init("https://silkdock.ai/v1/audio/speech");curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer " . getenv("SILKDOCK_API_KEY"), "Content-Type: application/json", ], CURLOPT_POSTFIELDS => json_encode([ "model" => "tts-1", "input" => "Hello, welcome to the text-to-speech service.", "voice" => "alloy", "response_format" => "mp3", ]),]);file_put_contents("output.mp3", curl_exec($ch));echo "Saved to output.mp3";<?phprequire_once "HTTP/Request2.php";$request = new HTTP_Request2("https://silkdock.ai/v1/audio/speech", HTTP_Request2::METHOD_POST);$request->setHeader([ "Authorization" => "Bearer " . getenv("SILKDOCK_API_KEY"), "Content-Type" => "application/json",]);$request->setBody(json_encode([ "model" => "tts-1", "input" => "Hello, world!", "voice" => "alloy",]));$response = $request->send();file_put_contents("speech.mp3", $response->getBody());echo "Saved to speech.mp3";<?phprequire "vendor/autoload.php";use GuzzleHttpClient;$client = new Client();$client->post("https://silkdock.ai/v1/audio/speech", [ "headers" => [ "Authorization" => "Bearer " . getenv("SILKDOCK_API_KEY"), "Content-Type" => "application/json", ], "json" => [ "model" => "tts-1", "input" => "Hello, world!", "voice" => "alloy", ], "sink" => "speech.mp3",]);echo "Saved to speech.mp3";<?php$client = new httpClient();$request = new httpClientRequest("POST", "https://silkdock.ai/v1/audio/speech");$request->setHeaders([ "Authorization" => "Bearer " . getenv("SILKDOCK_API_KEY"), "Content-Type" => "application/json",]);$body = new httpMessageBody();$body->append(json_encode([ "model" => "tts-1", "input" => "Hello, world!", "voice" => "alloy",]));$request->setBody($body);$client->enqueue($request)->send();$response = $client->getResponse();file_put_contents("speech.mp3", $response->getBody());echo "Saved to speech.mp3";import OpenAIlet client = OpenAI(configuration: .init( token: ProcessInfo.processInfo.environment["SILKDOCK_API_KEY"]!, host: "silkdock.ai", scheme: "https"))let query = AudioSpeechQuery( model: "tts-1", input: "Hello, welcome to the text-to-speech service.", voice: .alloy, responseFormat: .mp3)let result = try await client.audioCreateSpeech(query: query)try result.audio.write(to: URL(fileURLWithPath: "output.mp3"))print("Saved to output.mp3")import Foundationvar req = URLRequest(url: URL(string: "https://silkdock.ai/v1/audio/speech")!)req.httpMethod = "POST"req.setValue("Bearer \(ProcessInfo.processInfo.environment["SILKDOCK_API_KEY"]!)", forHTTPHeaderField: "Authorization")req.setValue("application/json", forHTTPHeaderField: "Content-Type")req.httpBody = try! JSONSerialization.data(withJSONObject: [ "model": "tts-1", "input": "Hello, welcome to the text-to-speech service.", "voice": "alloy", "response_format": "mp3",])let (data, _) = try! await URLSession.shared.data(for: req)try! data.write(to: URL(fileURLWithPath: "output.mp3"))print("Saved to output.mp3")using OpenAI;using OpenAI.Audio;var client = new AudioClient( model: "tts-1", credential: new System.ClientModel.ApiKeyCredential( Environment.GetEnvironmentVariable("SILKDOCK_API_KEY")!), options: new OpenAIClientOptions { Endpoint = new Uri("https://silkdock.ai/v1") });var result = await client.GenerateSpeechAsync( "Hello, welcome to the text-to-speech service.", GeneratedSpeechVoice.Alloy, new SpeechGenerationOptions { ResponseFormat = GeneratedSpeechFormat.Mp3 });await File.WriteAllBytesAsync("output.mp3", result.Value.ToArray());Console.WriteLine("Saved to output.mp3");using System.Net.Http;using System.Net.Http.Json;var client = new HttpClient();client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Environment.GetEnvironmentVariable("SILKDOCK_API_KEY")}");var res = await client.PostAsJsonAsync("https://silkdock.ai/v1/audio/speech", new { model = "tts-1", action = "tts", input = "Hello, welcome to the text-to-speech service.", voice = "alloy", response_format = "mp3",});await File.WriteAllBytesAsync("output.mp3", await res.Content.ReadAsByteArrayAsync());Console.WriteLine("Saved to output.mp3");require "openai"client = OpenAI::Client.new(access_token: ENV["SILKDOCK_API_KEY"],uri_base: "https://silkdock.ai/v1")response = client.audio.speech(parameters: { model: "tts-1", input: "Hello, welcome to the text-to-speech service.", voice: "alloy", response_format: "mp3"})File.binwrite("output.mp3", response)puts "Saved to output.mp3"require "net/http"require "json"uri = URI("https://silkdock.ai/v1/audio/speech")req = Net::HTTP::Post.new(uri)req["Authorization"] = "Bearer #{ENV['SILKDOCK_API_KEY']}"req["Content-Type"] = "application/json"req.body = {model: "tts-1", action: "tts",input: "Hello, welcome to the text-to-speech service.",voice: "alloy", response_format: "mp3"}.to_jsonres = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }File.binwrite("output.mp3", res.body)puts "Saved to output.mp3"import com.openai.client.OpenAIClientimport com.openai.client.okhttp.OpenAIOkHttpClientimport com.openai.models.*import java.nio.file.Filesimport java.nio.file.Pathval client: OpenAIClient = OpenAIOkHttpClient.builder() .apiKey(System.getenv("SILKDOCK_API_KEY")) .baseURL("https://silkdock.ai/v1") .build()val response = client.audio().speech().create( SpeechCreateParams.builder() .model(SpeechModel.TTS_1) .input("Hello, welcome to the text-to-speech service.") .voice(SpeechCreateParams.Voice.ALLOY) .build())Files.write(Path.of("output.mp3"), response.body().bytes())println("Saved to output.mp3")import java.net.http.*import java.net.URIimport java.nio.file.Filesimport java.nio.file.Pathval req = HttpRequest.newBuilder() .uri(URI.create("https://silkdock.ai/v1/audio/speech")) .header("Authorization", "Bearer ${System.getenv("SILKDOCK_API_KEY")}") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString( "input":"Hello, welcome to the text-to-speech service.", "voice":"alloy","response_format":"mp3"}""")) .build()val res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofByteArray())Files.write(Path.of("output.mp3"), res.body())println("Saved to output.mp3")use reqwest::blocking::Client;use serde_json::json;use std::fs;fn main() -> Result<(), Box<dyn std::error::Error>> { let bytes = Client::new() .post("https://silkdock.ai/v1/audio/speech") .header("Authorization", format!("Bearer {}", std::env::var("SILKDOCK_API_KEY")?)) .json(&json!({ "model": "tts-1", "input": "Hello, welcome to the text-to-speech service.", "voice": "alloy", "response_format": "mp3" })) .send()? .bytes()?; fs::write("output.mp3", &bytes)?; println!("Saved to output.mp3"); Ok(())}POST /v1/audio/speech HTTP/1.1Host: silkdock.aiAuthorization: Bearer <YOUR_API_KEY>Content-Type: application/json{"model": "tts-1","input": "Hello, welcome to the text-to-speech service.","voice": "alloy","response_format": "mp3"}import 'dart:io';import 'dart:convert';import 'package:http/http.dart' as http;void main() async {final res = await http.post( Uri.parse('https://silkdock.ai/v1/audio/speech'), headers: { 'Authorization': 'Bearer ${const String.fromEnvironment("SILKDOCK_API_KEY")}', 'Content-Type': 'application/json', }, body: jsonEncode({ 'model': 'tts-1', 'action': 'tts', 'input': 'Hello, welcome to the text-to-speech service.', 'voice': 'alloy', 'response_format': 'mp3', }),);await File('output.mp3').writeAsBytes(res.bodyBytes);print('Saved to output.mp3');}library(httr2)req <- request("https://silkdock.ai/v1/audio/speech") |>req_headers( Authorization = paste("Bearer", Sys.getenv("SILKDOCK_API_KEY")), "Content-Type" = "application/json") |>req_body_json(list( model = "tts-1", action = "tts", input = "Hello, welcome to the text-to-speech service.", voice = "alloy", response_format = "mp3"))resp <- req_perform(req)writeBin(resp_body_raw(resp), "output.mp3")cat("Saved to output.mp3")(* requires cohttp-lwt-unix *)open Cohttp_lwt_unixopen Cohttpopen Lwtlet () = "input":"Hello, welcome to the text-to-speech service.", "voice":"alloy","response_format":"mp3"}|} inlet headers = Header.of_list [ "Authorization", "Bearer " ^ Sys.getenv "SILKDOCK_API_KEY"; "Content-Type", "application/json";] inLwt_main.run ( Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) (Uri.of_string "https://silkdock.ai/v1/audio/speech") >>= fun (_, body) -> Cohttp_lwt.Body.to_string body >>= fun s -> let oc = open_out_bin "output.mp3" in output_string oc s; close_out oc; print_string "Saved to output.mp3"; return_unit)Response
The response body is raw binary audio. Save it directly to a file:
output.mp3forresponse_format: "mp3"(default)output.opusforresponse_format: "opus"output.aacforresponse_format: "aac"output.flacforresponse_format: "flac"output.wavforresponse_format: "wav"output.pcmforresponse_format: "pcm"
Last updated on