A comprehensive .NET 8 SDK for integrating with the ElevenLabs API. This SDK provides access to text-to-speech, voice management, speech-to-text, voice changing, and audio isolation features.
Install the package via NuGet:
dotnet add package Newtro.ElevenLabs.DotNet.SDKOr via the Package Manager Console:
Install-Package Newtro.ElevenLabs.DotNet.SDK// Add services to the DI container
services.AddElevenLabs(options =>
{
options.ApiKey = "your-api-key-here";
});
// Or create a client directly
var client = new ElevenLabsClient("your-api-key-here");// Get a list of available voices
var voices = await client.Voices.GetVoicesAsync();
// Convert text to speech
var request = new TextToSpeechRequest
{
Text = "Hello, world! This is ElevenLabs text-to-speech API.",
ModelId = "eleven_monolingual_v1",
VoiceSettings = new VoiceSettings
{
Stability = 0.5f,
SimilarityBoost = 0.75f
}
};
// Get the audio as a stream
using var audioStream = await client.TextToSpeech.TextToSpeechAsync(
"21m00Tcm4TlvDq8ikWAM", // Voice ID
request
);
// Save to file
using var fileStream = File.Create("output.mp3");
await audioStream.CopyToAsync(fileStream);// Get all available voices
var voices = await client.Voices.GetVoicesAsync();
// Get a specific voice
var voice = await client.Voices.GetVoiceAsync("21m00Tcm4TlvDq8ikWAM");
// Get voice settings
var settings = await client.Voices.GetVoiceSettingsAsync("21m00Tcm4TlvDq8ikWAM");
// Edit voice settings
await client.Voices.EditVoiceSettingsAsync("21m00Tcm4TlvDq8ikWAM", new VoiceSettings
{
Stability = 0.7f,
SimilarityBoost = 0.8f
});// Convert speech to text
using var fileStream = File.OpenRead("speech.mp3");
var text = await client.SpeechToText.SpeechToTextAsync(fileStream);
Console.WriteLine(text);// Change voice in an audio file
using var inputStream = File.OpenRead("input.mp3");
using var outputStream = await client.VoiceChanger.ChangeVoiceAsync(
inputStream,
"21m00Tcm4TlvDq8ikWAM", // Target voice ID
new VoiceSettings
{
Stability = 0.5f,
SimilarityBoost = 0.75f
}
);
// Save to file
using var fileStream = File.Create("changed_voice.mp3");
await outputStream.CopyToAsync(fileStream);// Isolate voice from an audio file
using var inputStream = File.OpenRead("mixed_audio.mp3");
using var outputStream = await client.AudioIsolation.IsolateVoiceAsync(inputStream);
// Save to file
using var fileStream = File.Create("isolated_voice.mp3");
await outputStream.CopyToAsync(fileStream);The SDK uses custom exceptions for error handling:
try
{
var voices = await client.Voices.GetVoicesAsync();
}
catch (ElevenLabsAuthenticationException ex)
{
// Handle authentication errors
Console.WriteLine($"Authentication failed: {ex.Message}");
}
catch (ElevenLabsRateLimitException ex)
{
// Handle rate limit errors
Console.WriteLine($"Rate limit exceeded: {ex.Message}");
}
catch (ElevenLabsException ex)
{
// Handle other API errors
}You can customize the HTTP client used by the SDK:
services.AddHttpClient<IElevenLabsHttpClient, ElevenLabsHttpClient>(client =>
{
client.DefaultRequestHeaders.Add("User-Agent", "MyApplication/1.0");
// Add any other custom HTTP client configuration
});
services.AddElevenLabs(options =>
{
options.ApiKey = "your-api-key-here";
});In an ASP.NET Core application, add the SDK to the services in Program.cs or Startup.cs:
builder.Services.AddElevenLabs(options =>
{
options.ApiKey = builder.Configuration["ElevenLabs:ApiKey"];
options.TimeoutSeconds = 60;
});Then inject the client into your controllers or services:
public class SpeechController : ControllerBase
{
private readonly IElevenLabsClient _elevenLabsClient;
public SpeechController(IElevenLabsClient elevenLabsClient)
{
_elevenLabsClient = elevenLabsClient;
}
[HttpPost("convert")]
public async Task<IActionResult> ConvertTextToSpeech([FromBody] TextToSpeechModel model)
{
try
{
var request = new TextToSpeechRequest
{
Text = model.Text,
ModelId = model.ModelId,
VoiceSettings = new VoiceSettings
{
Stability = model.Stability,
SimilarityBoost = model.SimilarityBoost
}
};
using var audioStream = await _elevenLabsClient.TextToSpeech.TextToSpeechAsync(
model.VoiceId,
request
);
return File(audioStream, "audio/mpeg", "speech.mp3");
}
catch (ElevenLabsException ex)
{
return BadRequest(new { error = ex.Message });
}
}
}The SDK provides the following main components:
The main entry point for the SDK, providing access to all services:
TextToSpeech: Text-to-speech conversion servicesVoices: Voice management servicesModels: Model management servicesSpeechToText: Speech-to-text conversion servicesVoiceChanger: Voice changing servicesAudioIsolation: Audio isolation services
TextToSpeechAsync: Converts text to speechStreamTextToSpeechAsync: Streams text-to-speech conversion
GetVoicesAsync: Gets all available voicesGetVoiceAsync: Gets a specific voiceGetDefaultVoiceSettingsAsync: Gets default voice settingsGetVoiceSettingsAsync: Gets settings for a specific voiceEditVoiceSettingsAsync: Edits settings for a specific voiceDeleteVoiceAsync: Deletes a voice
GetModelsAsync: Gets all available models
SpeechToTextAsync: Converts speech to text
ChangeVoiceAsync: Changes the voice in an audio fileStreamChangeVoiceAsync: Streams voice changing process
IsolateVoiceAsync: Isolates voice from an audio fileStreamIsolateVoiceAsync: Streams voice isolation process
This SDK is licensed under the MIT License. See the LICENSE file for details.