Add Full ElevenLabs API Support for Voice Selection via API Key in External Integrations

Check out this new update to custom functions: Today we released a crazy powerful update to Custom Functions: Full Virtual Machine Execution. | Sean Thielen

Here’s some code that will work for you!

import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";

export const handler = async () => {
  const client = new ElevenLabsClient({
    apiKey: ai.config.apiKey,
  });

  const stream = await client.textToSpeech.convert(ai.config.voiceId, {
    text: ai.config.inputText,
    model_id: ai.config.modelId,
  });

  if (!stream) {
    throw new Error('Audio generation failed');
  }

  const chunks = [];

  for await (const chunk of stream) {
    chunks.push(chunk);
  }

  const data = Buffer.concat(chunks);

  if (!data) {
    throw new Error('Audio generation failed');
  }

  // Upload data
  const uploadResult = await ai.uploadFile(data, "audio/mpeg");
  ai.vars[ai.config.outputVariableName] = uploadResult;
};