Skip to content

AssemblyAI/assemblyai-go-sdk

Repository files navigation


CI Passing GitHub License Go Reference AssemblyAI Twitter AssemblyAI YouTube Discord

AssemblyAI Go SDK

A Go client library for accessing AssemblyAI.

Overview

Documentation

Visit our AssemblyAI API Documentation to get an overview of our models!

See the reference docs at pkg.go.dev.

Quickstart

Installation

go get github.com/AssemblyAI/assemblyai-go-sdk

Examples

Before you begin, you need to have your API key. If you don't have one yet, sign up for one!

Core Transcription

Transcribe an audio file from URL
package main

import (
    "context"
    "log"
    "os"

    "github.com/AssemblyAI/assemblyai-go-sdk"
)

func main() {
    apiKey := os.Getenv("ASSEMBLYAI_API_KEY")

    ctx := context.Background()

    audioURL := "https://example.org/audio.mp3"

    client := assemblyai.NewClient(apiKey)

    transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, nil)
    if err != nil {
        log.Fatal("Something bad happened:", err)
    }

    log.Println(*transcript.Text)
}
Transcribe a local audio file
package main

import (
    "context"
    "log"
    "os"

    "github.com/AssemblyAI/assemblyai-go-sdk"
)

func main() {
    apiKey := os.Getenv("ASSEMBLYAI_API_KEY")

    ctx := context.Background()

    client := assemblyai.NewClient(apiKey)

    f, err := os.Open("./my-local-audio-file.wav")
    if err != nil {
        log.Fatal("Couldn't open audio file:", err)
    }
    defer f.Close()

    transcript, err := client.Transcripts.TranscribeFromReader(ctx, f, nil)
    if err != nil {
        log.Fatal("Something bad happened:", err)
    }

    log.Println(*transcript.Text)
}

Audio Intelligence

Identify entities in a transcript
package main

import (
    "context"
    "log"
    "os"

    "github.com/AssemblyAI/assemblyai-go-sdk"
)

func main() {
    apiKey := os.Getenv("ASSEMBLYAI_API_KEY")

    ctx := context.Background()

    audioURL := "https://example.org/audio.mp3"

    client := assemblyai.NewClient(apiKey)

    opts := &assemblyai.TranscriptParams{
        EntityDetection: assemblyai.Bool(true),
    }

    transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, opts)
    if err != nil {
        log.Fatal("Something bad happened:", err)
    }

    for _, entity := range transcript.Entities {
        log.Println(*entity.Text)
        log.Println(entity.EntityType)
        log.Printf("Timestamp: %v - %v", *entity.Start, *entity.End)
    }
}

Real-Time Transcription

Check out the realtime example.

Playgrounds

Visit one of our Playgrounds:

Tips and tricks

Inspect API errors

If you receive an API error, you can inspect the HTTP response returned by the API for more details:

transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, nil)
if err != nil {
    var apierr aai.APIError
    if errors.As(err, &apierr) {
        // apierr.Response is the *http.Response from the API call.
        fmt.Println(apierr.Response.StatusCode)
    } else {
        // err is not an API error.
    }
}