Skip to main content
1

API を呼び出す

import { CartesiaClient } from "@cartesia/cartesia-js";
import fs from "node:fs";
import { spawn } from "node:child_process";
import process from "node:process";

if (!process.env.CARTESIA_API_KEY) {
  throw new Error("CARTESIA_API_KEY is not set");
}

// Set up the client.
const client = new CartesiaClient({
  apiKey: process.env.CARTESIA_API_KEY,
});

// Make the API call.
const response = await client.tts.bytes({
  modelId: "sonic-3.5",
  // You can find more voices at https://play.cartesia.ai/voices
  voice: {
    mode: "id",
    id: "694f9389-aac1-45b6-b726-9d9369183238",
  },
  // You can find the supported `output_format`s at https://docs.cartesia.ai/api-reference/tts/bytes
  outputFormat: {
    container: "wav",
    encoding: "pcm_s16le",
    sampleRate: 44100,
  },
  transcript: "Welcome to Cartesia Sonic!",
});

// Write `response` to a file. (We convert the response to a Uint8Array first.)
fs.writeFileSync("sonic-3.wav", await new Response(response).bytes());

// Play the file.
spawn("ffplay", ["-autoexit", "-nodisp", "sonic-3.wav"]);
1

スクリプトを実行する

env CARTESIA_API_KEY=YOUR_API_KEY node hello.js
Cartesia API クライアントは、Bun や Deno などの他のランタイムにも対応しています。