Skip to content

Latest commit

 

History

History
94 lines (70 loc) · 5.43 KB

File metadata and controls

94 lines (70 loc) · 5.43 KB

ElevenLabs-DotNet-Proxy

NuGet version (ElevenLabs-DotNet-Proxy)

A simple Proxy API gateway for ElevenLabs-DotNet to make authenticated requests from a front end application without exposing your API keys.

Getting started

Install from NuGet

Install package ElevenLabs-DotNet-Proxy from Nuget. Here's how via command line:

Install-Package ElevenLabs-DotNet-Proxy

Documentation

Using either the ElevenLabs-DotNet or com.rest.elevenlabs packages directly in your front-end app may expose your API keys and other sensitive information. To mitigate this risk, it is recommended to set up an intermediate API that makes requests to ElevenLabs on behalf of your front-end app. This library can be utilized for both front-end and intermediary host configurations, ensuring secure communication with the ElevenLabs API.

Front End Example

In the front end example, you will need to securely authenticate your users using your preferred OAuth provider. Once the user is authenticated, exchange your custom auth token with your API key on the backend.

Follow these steps:

  1. Setup a new project using either the ElevenLabs-DotNet or com.rest.elevenlabs packages.
  2. Authenticate users with your OAuth provider.
  3. After successful authentication, create a new ElevenLabsAuthentication object and pass in the custom token.
  4. Create a new ElevenLabsClientSettings object and specify the domain where your intermediate API is located.
  5. Pass your new auth and settings objects to the ElevenLabsClient constructor when you create the client instance.

Here's an example of how to set up the front end:

var authToken = await LoginAsync();
var auth = new ElevenLabsAuthentication(authToken);
var settings = new ElevenLabsClientSettings(domain: "api.your-custom-domain.com");
var api = new ElevenLabsClient(auth, settings);

This setup allows your front end application to securely communicate with your backend that will be using the ElevenLabs-DotNet-Proxy, which then forwards requests to the ElevenLabs API. This ensures that your ElevenLabs API keys and other sensitive information remain secure throughout the process.

Back End Example

In this example, we demonstrate how to set up and use ElevenLabsProxyStartup in a new ASP.NET Core web app. The proxy server will handle authentication and forward requests to the ElevenLabs API, ensuring that your API keys and other sensitive information remain secure.

  1. Create a new ASP.NET Core minimal web API project.
  2. Add the ElevenLabs-DotNet nuget package to your project.
    • Powershell install: Install-Package ElevenLabs-DotNet-Proxy
    • Manually editing .csproj: <PackageReference Include="ElevenLabs-DotNet-Proxy" />
  3. Create a new class that inherits from AbstractAuthenticationFilter and override the ValidateAuthentication method. This will implement the IAuthenticationFilter that you will use to check user session token against your internal server.
  4. In Program.cs, create a new proxy web application by calling ElevenLabsProxyStartup.CreateWebApplication method, passing your custom AuthenticationFilter as a type argument.
  5. Create ElevenLabsAuthentication and ElevenLabsClientSettings as you would normally with your API keys, org id, or Azure settings.
public partial class Program
{
    private class AuthenticationFilter : AbstractAuthenticationFilter
    {
        public override void ValidateAuthentication(IHeaderDictionary request)
        {
            // You will need to implement your own class to properly test
            // custom issued tokens you've setup for your end users.
            if (!request["xi-api-key"].ToString().Contains(TestUserToken))
            {
                throw new AuthenticationException("User is not authorized");
            }
        }

        public override async Task ValidateAuthenticationAsync(IHeaderDictionary request)
        {
            await Task.CompletedTask; // remote resource call

            // You will need to implement your own class to properly test
            // custom issued tokens you've setup for your end users.
            if (!request["xi-api-key"].ToString().Contains(TestUserToken))
            {
                throw new AuthenticationException("User is not authorized");
            }
        }
    }

    public static void Main(string[] args)
    {
        var auth = ElevenLabsAuthentication.LoadFromEnv();
        var client = new ElevenLabsClient(auth);
        ElevenLabsProxyStartup.CreateWebApplication<AuthenticationFilter>(args, client).Run();
    }
}

Once you have set up your proxy server, your end users can now make authenticated requests to your proxy api instead of directly to the ElevenLabs API. The proxy server will handle authentication and forward requests to the ElevenLabs API, ensuring that your API keys and other sensitive information remain secure.