Using Custom Headers with SDKs

Learn how to use custom headers with the Deepgram SDKs.

JS SDK

The JS SDK provides the ability to set custom headers. This is optional and is only needed when a specific need arises.

Example

JavaScript
1const { createClient } = require("../../dist/main/index");
2const fs = require("fs");
3const path = require("path");
4
5const transcribeUrl = async () => {
6 const client = createClient("YOUR DEEPGRAM API KEY", {
7 global: {
8 fetch: { options: { headers: { "custom_header": "header_value" } } },
9 },
10 });
11
12 console.log("Transcribing URL", "https://dpgr.am/spacewalk.wav");
13 const { result, error } = await deepgram.listen.prerecorded.transcribeUrl(
14 {
15 url: "https://dpgr.am/spacewalk.wav",
16 },
17 {
18 model: "nova-3",
19 }
20 );
21
22 if (error) console.error(error);
23 if (!error) console.dir(result, { depth: 1 });
24};
25
26transcribeUrl();

Python SDK

The Python SDK provides the ability to set custom headers. This is optional and is only needed when a specific need arises.

Example

1# Install the SDK: pip install deepgram-sdk
2
3import os
4from dotenv import load_dotenv
5
6from deepgram import DeepgramClient, PrerecordedOptions
7
8load_dotenv()
9
10AUDIO_URL = {
11 "url": "https://dpgr.am/bueller.wav"
12}
13
14def main():
15 options: PrerecordedOptions = PrerecordedOptions(
16 model="nova-3"
17 )
18
19 # To demonstrate using the custom headers parameters, you could enable it like this
20 custom_headers: dict = {"custom_header": "header_value"}
21
22 # STEP 1 Create a Deepgram client using the API key in the environment variables DEEPGRAM_API_KEY
23 deepgram: DeepgramClient = DeepgramClient()
24
25 try:
26 # STEP 2 Call the transcribe_url method on the prerecorded class
27 response = deepgram.listen.rest.v("1").transcribe_url(
28 AUDIO_URL, options, headers=custom_headers
29 )
30 print(response.to_json(indent=4))
31 except Exception as e:
32 print(f"Exception: {e}")
33
34if __name__ == "__main__":
35 main()

.NET SDK

The .NET SDK provides the ability to set custom headers. This is optional and is only needed when a specific need arises.

Example

C#
1//Install the SDK: dotnet add package Deepgram
2
3using Deepgram.Models.Listen.v1.REST;
4
5namespace PreRecorded
6{
7 class Program
8 {
9 static async Task Main(string[] args)
10 {
11 // Initialize Library with default logging
12 // Normal logging is "Info" level
13 Library.Initialize();
14
15 // Set "DEEPGRAM_API_KEY" environment variable to your Deepgram API Key
16 var deepgramClient = ClientFactory.CreateListenRESTClient();
17
18 var prerecordedOptions = new PreRecordedSchema()
19 {
20 Model = "nova-3"
21 };
22
23 // but to demonstrate using the custom headers, you could enable it like this
24 var customHeaders = new Dictionary<string, string>();
25 customHeaders["custom_header"] = "header_value";
26
27 var response = await deepgramClient.TranscribeUrl(
28 new UrlSource("https://dpgr.am/bueller.wav"),
29 prerecordedOptions,
30 null, // Don't want to specify a cancellation token, use the default
31 null, // Placeholder for custom query parameters
32 customHeaders,
33 );
34
35 Console.WriteLine(response);
36
37 // Teardown Library
38 Library.Terminate();
39 }
40 }
41}

Go SDK

The Go SDK provides the ability to set custom headers. This is optional and is only needed when a specific need arises.

Example

Go
1// Install the SDK: go get github.com/deepgram/deepgram-go-sdk
2
3package main
4
5import (
6 "context"
7 "encoding/json"
8 "fmt"
9 "os"
10
11 prettyjson "github.com/hokaccha/go-prettyjson"
12
13 api "github.com/deepgram/deepgram-go-sdk/pkg/api/listen/v1/rest"
14 interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
15 client "github.com/deepgram/deepgram-go-sdk/pkg/client/listen"
16)
17
18const (
19 url string = "https://dpgr.am/bueller.wav"
20)
21
22func main() {
23 // init library
24 client.InitWithDefault()
25
26 // Go context
27 ctx := context.Background()
28
29 // set the Transcription options
30 options := &interfaces.PreRecordedTranscriptionOptions{
31 Model: "nova-3",
32 }
33
34 // create a Deepgram client
35 c := client.NewRESTWithDefaults()
36 dg := api.New(c)
37
38 // but to demonstrate using the custom headers, you could enable it like this
39 headers := http.Header{}
40 headers.Add("custom_header", "header_value")
41 ctx = interfaces.WithCustomHeaders(ctx, headers)
42
43 // send/process file to Deepgram
44 res, err := dg.FromURL(ctx, url, options)
45 if err != nil {
46 if e, ok := err.(*interfaces.StatusError); ok {
47 fmt.Printf("DEEPGRAM ERROR:\n%s:\n%s\n", e.DeepgramError.ErrCode, e.DeepgramError.ErrMsg)
48 }
49 fmt.Printf("FromStream failed. Err: %v\n", err)
50 os.Exit(1)
51 }
52
53 data, err := json.Marshal(res)
54 if err != nil {
55 fmt.Printf("json.Marshal failed. Err: %v\n", err)
56 os.Exit(1)
57 }
58
59 // make the JSON pretty
60 prettyJSON, err := prettyjson.Format(data)
61 if err != nil {
62 fmt.Printf("prettyjson.Marshal failed. Err: %v\n", err)
63 os.Exit(1)
64 }
65 fmt.Printf("\n\nResult:\n%s\n\n", prettyJSON)
66}
Built with