elevenlabs

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Api Go Library

fern shield

This is the documentation for the ElevenLabs API. You can use this API to use our service programmatically, this is done by using your API key. You can find your API key in the dashboard at https://elevenlabs.io/app/settings/api-keys.

Table of Contents

Reference

A full reference for this library is available here.

Installation

This SDK requires Go 1.22 or later.

go get github.com/elevenlabs/elevenlabs-go

Usage

import (
	context "context"
	elevenlabs "github.com/elevenlabs/elevenlabs-go"
	elevenlabsclient "github.com/elevenlabs/elevenlabs-go/client"
	types "github.com/elevenlabs/elevenlabs-go/types"
)

client := elevenlabsclient.NewClient()
client.History.List(
	context.TODO(),
	&types.ListHistoryRequest{
		PageSize: elevenlabs.Int(
			1,
		),
		StartAfterHistoryItemId: elevenlabs.String(
			"start_after_history_item_id",
		),
		VoiceId: elevenlabs.String(
			"voice_id",
		),
		ModelId: elevenlabs.String(
			"model_id",
		),
		DateBeforeUnix: elevenlabs.Int(
			1,
		),
		DateAfterUnix: elevenlabs.Int(
			1,
		),
		SortDirection: types.ListHistoryRequestSortDirectionAsc.Ptr(),
		Search: elevenlabs.String(
			"search",
		),
		Source: types.ListHistoryRequestSourceTts.Ptr(),
	},
)

Environments

This SDK allows you to configure a different environment for API requests by passing the WithBaseURL request option to the client.

import (
  "os"
  "github.com/elevenlabs/elevenlabs-go/client"
  "github.com/elevenlabs/elevenlabs-go/option"
  "github.com/elevenlabs/elevenlabs-go"
)

client := client.NewClient(
  option.WithApiKey(elevenlabs.String(os.Getenv("ELEVENLABS_API_KEY"))),
  option.WithBaseURL(elevenlabs.Environments.Production),
)

Available environments:

  • elevenlabs.Environments.Production
  • elevenlabs.Environments.ProductionUs
  • elevenlabs.Environments.ProductionEu
  • elevenlabs.Environments.ProductionIndia
  • elevenlabs.Environments.ProductionSingapore

WithBaseURL also accepts any URL string, such as a staging server or a local mock.

Errors

A non-2xx response returns an error wrapping a *core.APIError, which carries the status code and raw response body:

var apiErr *core.APIError
if errors.As(err, &apiErr) {
  fmt.Println(apiErr.StatusCode)
}

The core package also exposes typed errors for endpoints that declare them, each embedding *core.APIError so the status code is still reachable through it.

Request Options

Every method accepts variadic opts ...option.RequestOption to override behavior for a single call — a different base URL, additional headers, or a longer timeout:

response, err := client.SomeResource.SomeMethod(
  ctx,
  request,
  option.WithHTTPHeader(httpHeader),
  option.WithMaxAttempts(1),
)

Advanced

Custom Client

This SDK is built to work with any instance of *http.Client. By default, if no client is provided, the SDK constructs one. You can pass your own through option.WithHTTPClient:

customClient := &http.Client{}

client := client.NewClient(
  option.WithHTTPClient(customClient),
)
Timeouts
import (
  "os"
  "net/http"
  "time"
  "github.com/elevenlabs/elevenlabs-go/client"
  "github.com/elevenlabs/elevenlabs-go/option"
  "github.com/elevenlabs/elevenlabs-go"
)

client := client.NewClient(
  option.WithApiKey(elevenlabs.String(os.Getenv("ELEVENLABS_API_KEY"))),
  option.WithHTTPClient(
    &http.Client{
      Timeout: 10 * time.Second,
    },
  ),
)
WebSockets

client.TextToSpeech.Realtime() opens the connection and returns a *TextToSpeechSocket. Register handlers with its On... methods, call Start() to begin delivering messages, send with its typed Send... methods, and call Close() when done.

socket, err := client.TextToSpeech.Realtime(ctx, nil)
if err != nil {
  return err
}
defer socket.Close()

socket.OnReceiveMessage(func(message *types.ReceiveMessage) {
  // Handle each message.
})
socket.Start()

var payload types.SendMessage
if err := socket.SendSendMessage(ctx, &payload); err != nil {
  return err
}

The SDK has one socket per channel:

  • client.TextToSpeech.Realtime() returns a *TextToSpeechSocket
  • client.TextToDialogue.Realtime() returns a *TextToDialogueSocket
  • client.Dubbing.Live() returns a *DubbingSocket
  • client.SpeechToText.Realtime() returns a *SpeechToTextSocket
  • client.SpeechEngine.Upstream() returns a *SpeechEngineSocket
  • client.TextToDialogueMultiContext.Realtime() returns a *TextToDialogueMultiContextSocket
  • client.TextToSpeechMultiContext.Realtime() returns a *TextToSpeechMultiContextSocket
  • client.Translation.Realtime() returns a *TranslationSocket

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Environments = struct {
	Production          string
	ProductionUs        string
	ProductionEu        string
	ProductionIndia     string
	ProductionSingapore string
}{
	Production:          "https://api.elevenlabs.io",
	ProductionUs:        "https://api.us.elevenlabs.io",
	ProductionEu:        "https://api.eu.residency.elevenlabs.io",
	ProductionIndia:     "https://api.in.residency.elevenlabs.io",
	ProductionSingapore: "https://api.sg.residency.elevenlabs.io",
}

Environments defines all of the API environments. These values can be used with the WithBaseURL RequestOption to override the client's default environment, if any.

View Source
var ErrorCodes internal.ErrorCodes = internal.ErrorCodes{
	400: func(apiError *core.APIError) error {
		return &BadRequestError{
			APIError: apiError,
		}
	},
	401: func(apiError *core.APIError) error {
		return &UnauthorizedError{
			APIError: apiError,
		}
	},
	403: func(apiError *core.APIError) error {
		return &ForbiddenError{
			APIError: apiError,
		}
	},
	404: func(apiError *core.APIError) error {
		return &NotFoundError{
			APIError: apiError,
		}
	},
	409: func(apiError *core.APIError) error {
		return &ConflictError{
			APIError: apiError,
		}
	},
	422: func(apiError *core.APIError) error {
		return &UnprocessableEntityError{
			APIError: apiError,
		}
	},
	425: func(apiError *core.APIError) error {
		return &TooEarlyError{
			APIError: apiError,
		}
	},
}
View Source
var NewConfig = apiconfig.NewConfig

NewConfig creates a Config with spec-derived defaults.

View Source
var NewRetryConfig = apiconfig.NewRetryConfig

NewRetryConfig returns a RetryConfig initialized with spec-derived defaults.

View Source
var WithBaseURL = apiconfig.WithBaseURL

WithBaseURL returns a RequestOption that overrides BaseURL for a single request.

View Source
var WithRetryConfig = apiconfig.WithRetryConfig

WithRetryConfig returns a RequestOption that overrides the RetryConfig for a single request.

View Source
var WithTimeout = apiconfig.WithTimeout

WithTimeout returns a RequestOption that overrides Timeout for a single request.

Functions

func Bool

func Bool(b bool) *bool

Bool returns a pointer to the given bool value.

func Byte

func Byte(b byte) *byte

Byte returns a pointer to the given byte value.

func Bytes

func Bytes(b []byte) *[]byte

Bytes returns a pointer to the given []byte value.

func Complex64

func Complex64(c complex64) *complex64

Complex64 returns a pointer to the given complex64 value.

func Complex128

func Complex128(c complex128) *complex128

Complex128 returns a pointer to the given complex128 value.

func Float32

func Float32(f float32) *float32

Float32 returns a pointer to the given float32 value.

func Float64

func Float64(f float64) *float64

Float64 returns a pointer to the given float64 value.

func Int

func Int(i int) *int

Int returns a pointer to the given int value.

func Int8

func Int8(i int8) *int8

Int8 returns a pointer to the given int8 value.

func Int16

func Int16(i int16) *int16

Int16 returns a pointer to the given int16 value.

func Int32

func Int32(i int32) *int32

Int32 returns a pointer to the given int32 value.

func Int64

func Int64(i int64) *int64

Int64 returns a pointer to the given int64 value.

func MustParseDate

func MustParseDate(date string) time.Time

MustParseDate attempts to parse the given string as a date time.Time, and panics upon failure.

func MustParseDateTime

func MustParseDateTime(datetime string) time.Time

MustParseDateTime attempts to parse the given string as a datetime time.Time, and panics upon failure.

func Null

func Null[T any]() *param.Nullable[T]

Null returns a *param.Nullable[T] with IsNull set to true, signalling an explicit JSON null.

func Nullable

func Nullable[T any](v T) *param.Nullable[T]

Nullable returns a *param.Nullable[T] set to v — use for nullable fields with a value.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to v — use when no type-specific helper exists.

func Rune

func Rune(r rune) *rune

Rune returns a pointer to the given rune value.

func String

func String(s string) *string

String returns a pointer to the given string value.

func Time

func Time(t time.Time) *time.Time

Time returns a pointer to the given time.Time value.

func UUID

func UUID(u uuid.UUID) *uuid.UUID

UUID returns a pointer to the given uuid.UUID value.

func Uint

func Uint(u uint) *uint

Uint returns a pointer to the given uint value.

func Uint8

func Uint8(u uint8) *uint8

Uint8 returns a pointer to the given uint8 value.

func Uint16

func Uint16(u uint16) *uint16

Uint16 returns a pointer to the given uint16 value.

func Uint32

func Uint32(u uint32) *uint32

Uint32 returns a pointer to the given uint32 value.

func Uint64

func Uint64(u uint64) *uint64

Uint64 returns a pointer to the given uint64 value.

func Uintptr

func Uintptr(u uintptr) *uintptr

Uintptr returns a pointer to the given uintptr value.

Types

type ApiError

type ApiError[T any] struct {
	Err      error
	Data     *T
	Body     []byte
	Raw      *http.Response
	Metadata ApiErrorMetadata
}

ApiError wraps API errors with detailed metadata including status code, headers, and raw response. It implements the error interface and provides structured access to error information.

func NewApiError

func NewApiError[T any](transportError *httptransport.ErrorResponse[T]) *ApiError[T]

NewApiError creates a new ApiError from an internal transport error. It extracts error details, body, status code, and headers into a user-facing error structure.

func (*ApiError[T]) Error

func (e *ApiError[T]) Error() string

Error implements the error interface, returning the error message string.

func (*ApiError[T]) GetBody

func (e *ApiError[T]) GetBody() []byte

GetBody returns the raw response body bytes from the error response. Returns nil if no response body was received.

func (*ApiError[T]) GetData

func (e *ApiError[T]) GetData() *T

GetData returns the deserialized error response data. Returns nil if unmarshaling failed or the response body was empty.

func (*ApiError[T]) Unwrap

func (e *ApiError[T]) Unwrap() error

Unwrap returns the underlying error, enabling errors.Is and errors.As to traverse the chain.

type ApiErrorMetadata

type ApiErrorMetadata struct {
	Headers    map[string]string
	StatusCode int
}

ApiErrorMetadata contains HTTP metadata associated with an error response.

type ApiResponse

type ApiResponse[T any] struct {
	Data     T
	Raw      *http.Response
	Metadata ApiResponseMetadata
}

ApiResponse is the user-facing wrapper for API responses. It contains the deserialized data, raw HTTP response, and metadata like headers and status code.

func NewApiResponse

func NewApiResponse[T any](resp *httptransport.Response[T]) *ApiResponse[T]

NewApiResponse creates a new response wrapper from an internal transport response. Extracts data and metadata into a user-facing structure.

func (*ApiResponse[T]) GetData

func (r *ApiResponse[T]) GetData() T

GetData returns the deserialized response data.

func (ApiResponse[T]) String

func (r ApiResponse[T]) String() string

String returns a JSON representation of the response for debugging. Returns an error message if JSON marshaling fails.

type ApiResponseMetadata

type ApiResponseMetadata struct {
	Headers    map[string]string
	StatusCode int
}

ApiResponseMetadata contains HTTP metadata from the API response. Includes status code and headers for inspection and debugging.

type BadRequestError

type BadRequestError struct {
	*core.APIError
	Body any
}

Invalid request

func (*BadRequestError) GetBody

func (b *BadRequestError) GetBody() any

func (*BadRequestError) MarshalJSON

func (b *BadRequestError) MarshalJSON() ([]byte, error)

func (*BadRequestError) UnmarshalJSON

func (b *BadRequestError) UnmarshalJSON(data []byte) error

func (*BadRequestError) Unwrap

func (b *BadRequestError) Unwrap() error

type Config

type Config = apiconfig.Config

Config holds all configuration parameters for the SDK client.

type ConflictError

type ConflictError struct {
	*core.APIError
	Body any
}

A generation for this preview is already in progress. Retry the request.

func (*ConflictError) GetBody

func (c *ConflictError) GetBody() any

func (*ConflictError) MarshalJSON

func (c *ConflictError) MarshalJSON() ([]byte, error)

func (*ConflictError) UnmarshalJSON

func (c *ConflictError) UnmarshalJSON(data []byte) error

func (*ConflictError) Unwrap

func (c *ConflictError) Unwrap() error

type FileParam

type FileParam struct {
	io.Reader
	// contains filtered or unexported fields
}

FileParam is a file type suitable for multipart/form-data uploads.

func NewFileParam

func NewFileParam(
	reader io.Reader,
	filename string,
	contentType string,
	opts ...FileParamOption,
) *FileParam

NewFileParam returns a *FileParam type suitable for multipart/form-data uploads. All file upload endpoints accept a simple io.Reader, which is usually created by opening a file via os.Open.

However, some endpoints require additional metadata about the file such as a specific Content-Type or custom filename. FileParam makes it easier to create the correct type signature for these endpoints.

func (*FileParam) ContentType

func (f *FileParam) ContentType() string

func (*FileParam) Name

func (f *FileParam) Name() string

type FileParamOption

type FileParamOption interface {
	// contains filtered or unexported methods
}

FileParamOption adapts the behavior of the FileParam. No options are implemented yet, but this interface allows for future extensibility.

type ForbiddenError

type ForbiddenError struct {
	*core.APIError
	Body any
}

Permission denied

func (*ForbiddenError) GetBody

func (f *ForbiddenError) GetBody() any

func (*ForbiddenError) MarshalJSON

func (f *ForbiddenError) MarshalJSON() ([]byte, error)

func (*ForbiddenError) UnmarshalJSON

func (f *ForbiddenError) UnmarshalJSON(data []byte) error

func (*ForbiddenError) Unwrap

func (f *ForbiddenError) Unwrap() error

type NotFoundError

type NotFoundError struct {
	*core.APIError
	Body any
}

Dubbing not found

func (*NotFoundError) GetBody

func (n *NotFoundError) GetBody() any

func (*NotFoundError) MarshalJSON

func (n *NotFoundError) MarshalJSON() ([]byte, error)

func (*NotFoundError) UnmarshalJSON

func (n *NotFoundError) UnmarshalJSON(data []byte) error

func (*NotFoundError) Unwrap

func (n *NotFoundError) Unwrap() error

type RequestOption

type RequestOption = apiconfig.RequestOption

RequestOption is a function that configures a single request.

type RetryConfig

type RetryConfig = apiconfig.RetryConfig

RetryConfig holds all runtime-configurable retry parameters.

type TooEarlyError

type TooEarlyError struct {
	*core.APIError
	Body any
}

Dubbing not ready

func (*TooEarlyError) GetBody

func (t *TooEarlyError) GetBody() any

func (*TooEarlyError) MarshalJSON

func (t *TooEarlyError) MarshalJSON() ([]byte, error)

func (*TooEarlyError) UnmarshalJSON

func (t *TooEarlyError) UnmarshalJSON(data []byte) error

func (*TooEarlyError) Unwrap

func (t *TooEarlyError) Unwrap() error

type UnauthorizedError

type UnauthorizedError struct {
	*core.APIError
	Body any
}

Authentication required

func (*UnauthorizedError) GetBody

func (u *UnauthorizedError) GetBody() any

func (*UnauthorizedError) MarshalJSON

func (u *UnauthorizedError) MarshalJSON() ([]byte, error)

func (*UnauthorizedError) UnmarshalJSON

func (u *UnauthorizedError) UnmarshalJSON(data []byte) error

func (*UnauthorizedError) Unwrap

func (u *UnauthorizedError) Unwrap() error

type UnprocessableEntityError

type UnprocessableEntityError struct {
	*core.APIError
	Body *types.HttpValidationError
}

Validation Error

func (*UnprocessableEntityError) GetBody

func (*UnprocessableEntityError) MarshalJSON

func (u *UnprocessableEntityError) MarshalJSON() ([]byte, error)

func (*UnprocessableEntityError) UnmarshalJSON

func (u *UnprocessableEntityError) UnmarshalJSON(data []byte) error

func (*UnprocessableEntityError) Unwrap

func (u *UnprocessableEntityError) Unwrap() error

Directories

Path Synopsis
llm
ivc
pvc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL