Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export const elevenlabsTTS = async (req, res) => {
// https://platform.openai.com/docs/api-reference/images/create
// https://ale_annini-elevenlabstts.express.val.run/?args=[%22{\%22text\%22:\%22it%20beautiful\%22}%22]
const payload = {
text: req.query.text,
model_id: "eleven_monolingual_v1",
voice_settings: {
stability: 0.5,
similarity_boost: 0.5,
},
};
const voiceId = "21m00Tcm4TlvDq8ikWAM"; // https://api.elevenlabs.io/v1/voices
const audio = await fetch(
`https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`,
{
method: "POST",
headers: {
"Accept": "audio/mpeg",
"xi-api-key": process.env.ELEVENLABS_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
},
);
// const blob = await audio.blob();
res.set("Content-Type", "audio/mpeg");
res.send(audio);
// const url = URL.createObjectURL(blob);
// res.send(`<audio controls="controls" src="${blob}" type="audio/mp3" />`);
};
October 23, 2023