elevenlabs-client
v0.0.9
Published
Typesafe and straightforward fetch client for interacting with the ElevenLabs API using feature-fetch
Downloads
108
Readme
Status: Experimental
elevenlabs-client
is a typesafe and straightforward fetch client for interacting with the ElevenLabs API using feature-fetch
.
📖 Usage
🌟 Motivation
Create an ElevenLabs API client that works in a Remotion (ReactJs) environment.
⚖️ Alternatives
Create an ElevenLabs Client
Use createElvenLabsClient()
to create a client with your API key.
import { createElvenLabsClient } from 'elevenlabs-client';
const client = createElvenLabsClient({
apiKey: 'YOUR_API_KEY'
});
Error Handling
Errors can occur during API requests, and the client will return detailed error information. Possible error types include:
NetworkError
: Indicates a failure in network communication, such as loss of connectivityRequestError
: Occurs when the server returns a response with a status code indicating an error (e.g., 4xx or 5xx)FetchError
: A general exception type that can encompass other error scenarios not covered byNetworkError
orRequestError
, for example when the response couldn't be parsed, ..
const voicesResult = await client.getVoices();
// First Approach: Handle error using `isErr()`
if (voicesResult.isErr()) {
const { error } = voicesResult;
if (error instanceof NetworkError) {
console.error('Network error:', error.message);
} else if (error instanceof RequestError) {
console.error('Request error:', error.message, 'Status:', error.status);
} else if (error instanceof FetchError) {
console.error('Service error:', error.message, 'Code:', error.code);
} else {
console.error('Unexpected error:', error);
}
}
// Second Approach: Unwrap response with `try-catch`
try {
const voices = voicesResult.unwrap();
} catch (error) {
if (error instanceof NetworkError) {
console.error('Network error:', error.message);
} else if (error instanceof RequestError) {
console.error('Request error:', error.message, 'Status:', error.status);
} else if (error instanceof FetchError) {
console.error('Service error:', error.message, 'Code:', error.code);
} else {
console.error('Unexpected error:', error);
}
}