Using SDKs with Self-Hosted

Learn about how to use Deepgram SDKs with Deepgram self-hosted deployments.

By default, Deepgram's SDKs hit the hosted endpoint api.deepgram.com. To use one of our SDKs with your self-hosted deployment, you will need to specify your own self-hosted endpoint instead.

Determining your host URL

If running your requests locally on your server, your URL may be as simple as http://localhost:8080 (pre-recorded) or ws://localhost:8080 (streaming).

If running your requests on other servers, you may be using a static IP, such as http://172.23.0.1:8080.

If you use a multi-server or auto-scaled environment, you may configure a URL that is served by a load balancer that routes your requests across several instances.

Note that for a host that does not have TLS enabled, you will use http rather than https, and ws rather than wss.

Python SDK

When deploying in a containerized environment, you can set client configuration parameters using the class ClientOptionsFromEnv. This will set configuration options in DeepgramClientOptions, obtaining those inputs from environments variables. Set the DEEPGRAM_HOST environment variable to direct your requests to your self-hosted rather than the hosted API.

Alternatively, you may also pass the url parameter as input in code to the DeepgramClientOptions constructor.

Below is an example of how to make your first API request to your self-hosted deployment using the Python SDK. Substitute your own host address in place of localhost if needed.

Note that the api_key field cannot be a blank string as it is a required parameter for the Python SDK, but it does not need to be a valid Deepgram API key, as authorization with Deepgram for self-hosted deployments is configured through the container, not at the individual request level.

from deepgram import DeepgramClient, ClientOptionsFromEnv, PrerecordedOptions

deepgram = DeepgramClient(config=ClientOptionsFromEnv(api_key="a", url="http://localhost:8080"))
response = deepgram.listen.prerecorded.v("1").transcribe_url(
  {
    "url": "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav"
  },
  PrerecordedOptions(
    model="nova-2",
    smart_format=True
  ),
)
print(response)

.NET SDK

The .NET SDK provides DeepgramHttpClientOptions (pre-recorded) and DeepgramWsClientOptions (streaming) classes, through which you can pass your host address. Below is a streaming example. Note that you should provide the /v1 suffix to the base address.

var apiKey = "<your API key>";
var options = new DeepgramWsClientOptions(){
  BaseAddress = "ws://localhost:8080/v1"
};
var liveClient = new LiveClient(apiKey, options);

If you encounter the following error message, note that you are receiving a 400 (bad request) when a 101 (successful stream) is expected.

Error: "The server returned status code \u0027400\u0027 when status code \u0027101\u0027 was expected."

Ensure that your request parameters are specifying a model that you have available on your self-hosted instance. For example, when you intend to serve requests through a formatted Nova-2 model, specify the following params:

var liveSchema = new LiveSchema()
{
  Model = "nova-2",
  SmartFormat = true,
};

If that model is not present in your models/ directory, the above error will occur.

Go SDK

To modify the Host option in type-client.go:

// ClientOptions defines any options for the client
type ClientOptions struct {
	...
	Host       string // override for the host endpoint
	...
	SelfHosted bool   // set to true if using self-hosted
  ...
}

You can create an object of type ClientOptions and then set the Host value.

	// create a Deepgram client
	c := client.New("", interfaces.ClientOptions{
		Host: "http://localhost:8080",
    SelfHosted: true,
	})

JavaScript SDK

To modify the URL, you can pass the url property within the global object to a new client.

const options: DeepgramClientOptions = {
  global: {
    url: "http://localhost:8080", // Set the desired URL here
  }
};

const client = createClient("DEEPGRAM_API_KEY", options);