Using Deepgram Speech-to-Text from the Go SDK
When to use this product
Use this skill for pkg/client/listen work:
- prerecorded transcription with
FromURL, FromFile, or FromStream
- live transcription with
pkg/client/listen/v1/websocket
- channel-based or callback-based streaming flows
Use a different skill when:
- you need TTS output (
deepgram-go-text-to-speech)
- you need text analysis on plain text (
deepgram-go-text-intelligence)
- you need analytics overlays like summaries, topics, or sentiments (
deepgram-go-audio-intelligence)
- you need Flux / conversational STT v2 (
deepgram-go-conversational-stt)
Authentication
Set DEEPGRAM_API_KEY before constructing clients.
export DEEPGRAM_API_KEY="your_api_key"
This SDK reads env-backed defaults via the client option layer. Prefer API key or token auth supported by the repo's client options; do not hardcode credentials.
Quick start
Prerecorded REST:
package main
import (
"context"
"fmt"
"log"
api "github.com/deepgram/deepgram-go-sdk/v3/pkg/api/listen/v1/rest"
listen "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/listen"
interfaces "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/interfaces"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx := context.Background()
client := listen.NewRESTWithDefaults()
dg := api.New(client)
resp, err := dg.FromURL(
ctx,
"https://dpgr.am/spacewalk.wav",
&interfaces.PreRecordedTranscriptionOptions{
Model: "nova-3",
SmartFormat: true,
Punctuate: true,
},
)
if err != nil {
return err
}
fmt.Println(resp.Results.Channels[0].Alternatives[0].Transcript)
return nil
}
Live WebSocket with channel fan-out:
package main
import (
"context"
"fmt"
"log"
listenws "github.com/deepgram/deepgram-go-sdk/v3/pkg/api/listen/v1/websocket"
listen "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/listen"
interfaces "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/interfaces"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx := context.Background()
handler := listenws.NewDefaultChanHandler()
conn, err := listen.NewWSUsingChanWithDefaults(
ctx,
&interfaces.LiveTranscriptionOptions{Model: "nova-3", InterimResults: true},
handler,
)
if err != nil {
return err
}
defer conn.Stop()
if ok := conn.Connect(); !ok {
return fmt.Errorf("connect failed")
}
conn.Start()
// The handler receives Open/Message/Metadata/UtteranceEnd events.
// In a real app, stream PCM/audio chunks from your mic or file reader here.
// For example (pseudo-code):
// for chunk := range audioChunks {
// if err := conn.WriteBinary(chunk); err != nil { return err }
// }
//
// When the input stream ends, flush any trailing audio and close cleanly:
// if err := conn.Finalize(); err != nil { return err }
return nil
}
Key parameters
interfaces.PreRecordedTranscriptionOptions
- common fields:
Model, Language, Punctuate, SmartFormat, Diarize, DiarizeModel (batch diarization version: latest/v1/v2), Redact, Utterances
- use with
pkg/api/listen/v1/rest: api.New(client).FromURL, FromFile, FromStream
interfaces.LiveTranscriptionOptions
- common fields:
Model, Language, Encoding, SampleRate, Channels, InterimResults, Endpointing
- constructor families
- REST:
listen.NewRESTWithDefaults(), listen.NewREST(apiKey, options)
- WS callbacks:
listen.NewWSUsingCallback...
- WS channels:
listen.NewWSUsingChan...
- lifecycle
Connect() returns bool; call Start(), stream/write audio, KeepAlive() as needed, Finalize(), then defer conn.Stop()
API reference (layered)
- In-repo reference
README.md
docs.go
pkg/client/listen/client.go
pkg/client/listen/v1/rest/client.go
pkg/client/listen/v1/websocket/client_callback.go
pkg/client/listen/v1/websocket/client_channel.go
pkg/client/interfaces/v1/types-prerecorded.go
pkg/client/interfaces/v1/types-stream.go
- OpenAPI
https://developers.deepgram.com/openapi.yaml
- AsyncAPI
https://developers.deepgram.com/asyncapi.yaml
- Context7
/llmstxt/developers_deepgram_llms_txt
- Product docs
https://developers.deepgram.com/reference/speech-to-text/listen-pre-recorded
https://developers.deepgram.com/reference/speech-to-text/listen-streaming
https://developers.deepgram.com/docs/speech-to-text
Gotchas
- This repo uses
listen package names for STT v1, not transcription.
- Streaming code is split into callback and channel variants; copy the style that matches the surrounding package.
- For WebSockets, pass a handler into
NewWSUsingChan..., keep defer conn.Stop() near construction, and finalize before shutdown.
- Live and prerecorded option structs are different; do not assume analytics-only prerecorded fields exist in live mode.
- Use
context.Context and return error; do not translate examples into exception-style control flow.
Example files in this repo
examples/speech-to-text/rest/url/main.go
examples/speech-to-text/rest/file/main.go
examples/speech-to-text/websocket/microphone_channel/main.go
examples/speech-to-text/websocket/microphone_callback/main.go
tests/edge_cases/keepalive/main.go
tests/edge_cases/reconnect_client/main.go
Central product skills
For cross-language Deepgram product knowledge — the consolidated API reference, documentation finder, focused runnable recipes, third-party integration examples, and MCP setup — install the central skills:
npx skills add deepgram/skills
This SDK ships language-idiomatic code skills; deepgram/skills ships cross-language product knowledge (see api, docs, recipes, examples, starters, setup-mcp).
1---2name: deepgram-go-speech-to-text3description: Use when writing or reviewing Go code in this repo that transcribes prerecorded audio with Listen v1 REST or streams live audio with Listen v1 WebSockets. Route text generation to deepgram-go-text-to-speech, text analysis to deepgram-go-text-intelligence, audio analytics overlays to deepgram-go-audio-intelligence, and Flux or other v2 conversational work to deepgram-go-conversational-stt.4---56# Using Deepgram Speech-to-Text from the Go SDK78## When to use this product910Use this skill for `pkg/client/listen` work:1112- prerecorded transcription with `FromURL`, `FromFile`, or `FromStream`13- live transcription with `pkg/client/listen/v1/websocket`14- channel-based or callback-based streaming flows1516Use a different skill when:1718- you need TTS output (`deepgram-go-text-to-speech`)19- you need text analysis on plain text (`deepgram-go-text-intelligence`)20- you need analytics overlays like summaries, topics, or sentiments (`deepgram-go-audio-intelligence`)21- you need Flux / conversational STT v2 (`deepgram-go-conversational-stt`)2223## Authentication2425Set `DEEPGRAM_API_KEY` before constructing clients.2627```bash28export DEEPGRAM_API_KEY="your_api_key"29```3031This SDK reads env-backed defaults via the client option layer. Prefer API key or token auth supported by the repo's client options; do not hardcode credentials.3233## Quick start3435Prerecorded REST:3637```go38package main3940import (41 "context"42 "fmt"43 "log"4445 api "github.com/deepgram/deepgram-go-sdk/v3/pkg/api/listen/v1/rest"46 listen "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/listen"47 interfaces "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/interfaces"48)4950func main() {51 if err := run(); err != nil {52 log.Fatal(err)53 }54}5556func run() error {57 ctx := context.Background()5859 client := listen.NewRESTWithDefaults()60 dg := api.New(client)6162 resp, err := dg.FromURL(63 ctx,64 "https://dpgr.am/spacewalk.wav",65 &interfaces.PreRecordedTranscriptionOptions{66 Model: "nova-3",67 SmartFormat: true,68 Punctuate: true,69 },70 )71 if err != nil {72 return err73 }7475 fmt.Println(resp.Results.Channels[0].Alternatives[0].Transcript)76 return nil77}78```7980Live WebSocket with channel fan-out:8182```go83package main8485import (86 "context"87 "fmt"88 "log"8990 listenws "github.com/deepgram/deepgram-go-sdk/v3/pkg/api/listen/v1/websocket"91 listen "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/listen"92 interfaces "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/interfaces"93)9495func main() {96 if err := run(); err != nil {97 log.Fatal(err)98 }99}100101func run() error {102 ctx := context.Background()103 handler := listenws.NewDefaultChanHandler()104105 conn, err := listen.NewWSUsingChanWithDefaults(106 ctx,107 &interfaces.LiveTranscriptionOptions{Model: "nova-3", InterimResults: true},108 handler,109 )110 if err != nil {111 return err112 }113 defer conn.Stop()114115 if ok := conn.Connect(); !ok {116 return fmt.Errorf("connect failed")117 }118119 conn.Start()120121 // The handler receives Open/Message/Metadata/UtteranceEnd events.122 // In a real app, stream PCM/audio chunks from your mic or file reader here.123 // For example (pseudo-code):124 // for chunk := range audioChunks {125 // if err := conn.WriteBinary(chunk); err != nil { return err }126 // }127 //128 // When the input stream ends, flush any trailing audio and close cleanly:129 // if err := conn.Finalize(); err != nil { return err }130131 return nil132}133```134135## Key parameters136137- `interfaces.PreRecordedTranscriptionOptions`138 - common fields: `Model`, `Language`, `Punctuate`, `SmartFormat`, `Diarize`, `DiarizeModel` (batch diarization version: `latest`/`v1`/`v2`), `Redact`, `Utterances`139 - use with `pkg/api/listen/v1/rest`: `api.New(client).FromURL`, `FromFile`, `FromStream`140- `interfaces.LiveTranscriptionOptions`141 - common fields: `Model`, `Language`, `Encoding`, `SampleRate`, `Channels`, `InterimResults`, `Endpointing`142- constructor families143 - REST: `listen.NewRESTWithDefaults()`, `listen.NewREST(apiKey, options)`144 - WS callbacks: `listen.NewWSUsingCallback...`145 - WS channels: `listen.NewWSUsingChan...`146- lifecycle147 - `Connect()` returns `bool`; call `Start()`, stream/write audio, `KeepAlive()` as needed, `Finalize()`, then `defer conn.Stop()`148149## API reference (layered)1501511. In-repo reference152 - `README.md`153 - `docs.go`154 - `pkg/client/listen/client.go`155 - `pkg/client/listen/v1/rest/client.go`156 - `pkg/client/listen/v1/websocket/client_callback.go`157 - `pkg/client/listen/v1/websocket/client_channel.go`158 - `pkg/client/interfaces/v1/types-prerecorded.go`159 - `pkg/client/interfaces/v1/types-stream.go`1602. OpenAPI161 - `https://developers.deepgram.com/openapi.yaml`1623. AsyncAPI163 - `https://developers.deepgram.com/asyncapi.yaml`1644. Context7165 - `/llmstxt/developers_deepgram_llms_txt`1665. Product docs167 - `https://developers.deepgram.com/reference/speech-to-text/listen-pre-recorded`168 - `https://developers.deepgram.com/reference/speech-to-text/listen-streaming`169 - `https://developers.deepgram.com/docs/speech-to-text`170171## Gotchas1721731. This repo uses `listen` package names for STT v1, not `transcription`.1742. Streaming code is split into callback and channel variants; copy the style that matches the surrounding package.1753. For WebSockets, pass a handler into `NewWSUsingChan...`, keep `defer conn.Stop()` near construction, and finalize before shutdown.1764. Live and prerecorded option structs are different; do not assume analytics-only prerecorded fields exist in live mode.1775. Use `context.Context` and return `error`; do not translate examples into exception-style control flow.178179## Example files in this repo180181- `examples/speech-to-text/rest/url/main.go`182- `examples/speech-to-text/rest/file/main.go`183- `examples/speech-to-text/websocket/microphone_channel/main.go`184- `examples/speech-to-text/websocket/microphone_callback/main.go`185- `tests/edge_cases/keepalive/main.go`186- `tests/edge_cases/reconnect_client/main.go`187188## Central product skills189190For cross-language Deepgram product knowledge — the consolidated API reference, documentation finder, focused runnable recipes, third-party integration examples, and MCP setup — install the central skills:191192```bash193npx skills add deepgram/skills194```195196This SDK ships language-idiomatic code skills; `deepgram/skills` ships cross-language product knowledge (see `api`, `docs`, `recipes`, `examples`, `starters`, `setup-mcp`).