# elevenlabs-ts
`elevenlabs-ts` is a TypeScript client library for interfacing with the [Eleven Labs](https://elevenlabs.io/?from=partnermorris7332) API,
which offers a range of services related to voice synthesis and text-to-speech (TTS) capabilities.
Requirements
To use this package you will need an elevanlabs api, if you don't one have one create one:
Then tap on the Profile Image, there you can generate API details elevan labs > profile, API key
Elevan labs has a free tier, with high quality audio and up to 10,000 characters, for commercial use upgrade to paid tier
Installation
To use the elevenlabs-ts
package in your project, run:
npm install elevenlabs-ts
Or if you prefer using Yarn:
yarn add elevenlabs-ts
Usage
To get started with the Eleven Labs API client, you need to create an instance of the ElevenLabsAPI
class with your API key.
import ElevenLabsAPI from 'elevenlabs-ts';
import fs from 'fs-extra';
const apiKey = 'YOUR_API_KEY';
const elevenLabsAPI = new ElevenLabsAPI(apiKey);
/// to get Voices
const voices: Voice[] = await elevenLabs.getVoices();
// to get single voice
const voice: Voice = await elevenLabs.getVoice('voiceId')
// to convert text to speech
const audio_response: Buffer = await elevenLabs.textToSpeech(VOICE_ID, { text: inputText })
// to save to local disk
fs.writeFile('audio.mp3', audio_response)
Methods
Here is an overview of the methods available in the ElevenLabsAPI
class:
Text-to-Speech
streamTextToSpeech(voiceId: string, options: TextToSpeechOptions): Promise<NodeJS.ReadableStream>
textToSpeech(voiceId: string, options: TextToSpeechOptions): Promise<Buffer>
Voice Management
getVoices(): Promise<Voice[]>
getVoice(voiceId: string, withSettings?: boolean): Promise<Voice>
addVoice(data: AddVoiceRequest): Promise<void>
editVoice(voiceId: string, data: EditVoiceRequest): Promise<void>
deleteVoice(voiceId: string): Promise<void>
getVoiceSettings(voiceId: string): Promise<VoiceSettings>
editVoiceSettings(voiceId: string, settings: VoiceSettings): Promise<void>
User Information
getUserInfo(): Promise<UserInfo>
getUserSubscriptionInfo(): Promise<UserSubscriptionInfo>
History
getHistoryItemAudio(historyItemId: string): Promise<HistoryAudio>
getHistoryItem(historyItemId: string): Promise<HistoryItem>
deleteHistoryItem(historyItemId: string): Promise<void>
downloadHistoryItems(historyItemIds: string[]): Promise<Buffer>
Models
getModels(): Promise<ModelInfo[]>
Default Voice Settings
getDefaultVoiceSettings(): Promise<VoiceSettings>
Example
Here's an example of converting text to speech:
async function convertTextToSpeech(text: string) {
const voiceId = 'voice-id'; // Replace with a valid voice ID
const writer = fs.createWriteStream('stream.mp3')
const stream = await elevenLabsAPI.streamTextToSpeech(voiceId, {text: text});
// Handle the stream, e.g., save to a file or play audio
writer.pipe(stream)
}
convertTextToSpeech('Hello, world!');
async function textToSpeech(text: string) {
const voiceId = 'voice-id'; // Replace with a valid voice ID
const response: Buffer = await elevenLabsAPI.textToSpeech(voiceId, {text: text});
// Handle the stream, e.g., save to a file or play audio
fs.createFile('audio.mp3', reponse)
}
textToSpeech('Hello, world!');
Environment Variables
The library uses the following environment variable for the API base URL:
-
API_BASE_URL
- The base URL for the Eleven Labs API. If not set, the default ishttps://api.elevenlabs.io/v1
.
Response and Error Handling
All methods return a promise that resolves to the requested data or throws an error in case of failure. Make sure to handle these appropriately in your application.
Contributing
We welcome contributions to the elevenlabs-ts
package. Please read the contributing guidelines before submitting your PR.
License
This project is licensed under the MIT License.
Disclaimer
This package is not officially maintained by Eleven Labs. Please use it at your own risk. All links to Eleven Labs may result in an affiliate commission
Make sure to replace `YOUR_API_KEY` with the actual API key provided to you by