A Go client library for accessing AssemblyAI.
Visit our AssemblyAI API Documentation to get an overview of our models!
See the reference docs at pkg.go.dev.
go get github.com/AssemblyAI/assemblyai-go-sdk
Before you begin, you need to have your API key. If you don't have one yet, sign up for one!
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)
}
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)
}
}
Check out the realtime example.
Visit one of our Playgrounds:
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.
}
}