The Livepeer Go library provides convenient access to the Livepeer Studio API from applications written in Golang.
For full documentation and examples, please visit docs.livepeer.org.
Livepeer API Reference: Welcome to the Livepeer API reference docs. Here you will find all the endpoints exposed on the standard Livepeer API, learn how to use them and what they return.
- SDK Installation
- SDK Example Usage
- Available Resources and Operations
- Retries
- Error Handling
- Server Selection
- Custom HTTP Client
- Authentication
- Special Types
To add the SDK as a dependency to your project:
go get github.com/livepeer/livepeer-go
package main
import (
"context"
livepeer "github.com/livepeer/livepeer-go"
"github.com/livepeer/livepeer-go/models/components"
"log"
)
func main() {
s := livepeer.New(
livepeer.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
)
ctx := context.Background()
res, err := s.Stream.Create(ctx, components.NewStreamPayload{
Name: "test_stream",
})
if err != nil {
log.Fatal(err)
}
if res.Stream != nil {
// handle response
}
}
Available methods
- Create - Create a signing key
- GetAll - Retrieves signing keys
- Delete - Delete Signing Key
- Get - Retrieves a signing key
- Update - Update a signing key
- GetAll - Retrieve assets
- Create - Upload an asset
- CreateViaURL - Upload asset via URL
- Get - Retrieves an asset
- Update - Patch an asset
- Delete - Delete an asset
- TextToImage - Text To Image
- ImageToImage - Image To Image
- ImageToVideo - Image To Video
- Upscale - Upscale
- AudioToText - Audio To Text
- SegmentAnything2 - Segment Anything 2
- GetRealtimeViewership - Query realtime viewership
- GetViewership - Query viewership metrics
- GetCreatorViewership - Query creator viewership metrics
- GetPublicViewership - Query public total views metrics
- GetUsage - Query usage metrics
- GetAll - Retrieve Multistream Targets
- Create - Create a multistream target
- Get - Retrieve a multistream target
- Update - Update Multistream Target
- Delete - Delete a multistream target
- Get - Retrieve Playback Info
Create- Create a room⚠️ DeprecatedGet- Retrieve a room⚠️ DeprecatedDelete- Delete a room⚠️ DeprecatedStartEgress- Start room RTMP egress⚠️ DeprecatedStopEgress- Stop room RTMP egress⚠️ DeprecatedCreateUser- Create a room user⚠️ DeprecatedGetUser- Get user details⚠️ DeprecatedUpdateUser- Update a room user⚠️ DeprecatedDeleteUser- Remove a user from the room⚠️ Deprecated
- GetClips - Retrieve clips of a session
- GetAll - Retrieve sessions
- Get - Retrieve a session
- GetRecorded - Retrieve Recorded Sessions
- Create - Create a stream
- GetAll - Retrieve streams
- Get - Retrieve a stream
- Update - Update a stream
- Delete - Delete a stream
- Terminate - Terminates a live stream
- StartPull - Start ingest for a pull stream
- CreateClip - Create a clip
- GetClips - Retrieve clips of a livestream
- AddMultistreamTarget - Add a multistream target
- RemoveMultistreamTarget - Remove a multistream target
- Create - Transcode a video
Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both. When specified by the OpenAPI spec document, the SDK will return the appropriate subclass.
Error Object | Status Code | Content Type |
---|---|---|
sdkerrors.Error | 404 | application/json |
sdkerrors.SDKError | 4xx-5xx | / |
package main
import (
"context"
"errors"
livepeergo "github.com/livepeer/livepeer-go"
"github.com/livepeer/livepeer-go/models/sdkerrors"
"log"
)
func main() {
s := livepeergo.New(
livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
)
ctx := context.Background()
res, err := s.Playback.Get(ctx, "<id>")
if err != nil {
var e *sdkerrors.Error
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.SDKError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
}
}
The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
The built-in net/http
client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.
import (
"net/http"
"time"
"github.com/myorg/your-go-sdk"
)
var (
httpClient = &http.Client{Timeout: 30 * time.Second}
sdkClient = sdk.New(sdk.WithClient(httpClient))
)
This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.
This SDK supports the following security scheme globally:
Name | Type | Scheme |
---|---|---|
APIKey |
http | HTTP Bearer |
You can configure it using the WithSecurity
option when initializing the SDK client instance. For example:
package main
import (
"context"
livepeergo "github.com/livepeer/livepeer-go"
"github.com/livepeer/livepeer-go/models/components"
"log"
)
func main() {
s := livepeergo.New(
livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
)
ctx := context.Background()
res, err := s.Stream.Create(ctx, components.NewStreamPayload{
Name: "test_stream",
Pull: &components.Pull{
Source: "https://myservice.com/live/stream.flv",
Headers: map[string]string{
"Authorization": "Bearer 123",
},
Location: &components.Location{
Lat: 39.739,
Lon: -104.988,
},
},
PlaybackPolicy: &components.PlaybackPolicy{
Type: components.TypeWebhook,
WebhookID: livepeergo.String("1bde4o2i6xycudoy"),
WebhookContext: map[string]any{
"streamerId": "my-custom-id",
},
RefreshInterval: livepeergo.Float64(600),
},
Profiles: []components.FfmpegProfile{
components.FfmpegProfile{
Width: 1280,
Name: "720p",
Height: 720,
Bitrate: 3000000,
Fps: 30,
FpsDen: livepeergo.Int64(1),
Quality: livepeergo.Int64(23),
Gop: livepeergo.String("2"),
Profile: components.ProfileH264Baseline.ToPointer(),
},
},
Record: livepeergo.Bool(false),
RecordingSpec: &components.NewStreamPayloadRecordingSpec{
Profiles: []components.TranscodeProfile{
components.TranscodeProfile{
Width: livepeergo.Int64(1280),
Name: livepeergo.String("720p"),
Height: livepeergo.Int64(720),
Bitrate: 3000000,
Quality: livepeergo.Int64(23),
Fps: livepeergo.Int64(30),
FpsDen: livepeergo.Int64(1),
Gop: livepeergo.String("2"),
Profile: components.TranscodeProfileProfileH264Baseline.ToPointer(),
Encoder: components.TranscodeProfileEncoderH264.ToPointer(),
},
},
},
Multistream: &components.Multistream{
Targets: []components.Target{
components.Target{
Profile: "720p",
VideoOnly: livepeergo.Bool(false),
ID: livepeergo.String("PUSH123"),
Spec: &components.TargetSpec{
Name: livepeergo.String("My target"),
URL: "rtmps://live.my-service.tv/channel/secretKey",
},
},
},
},
})
if err != nil {
log.Fatal(err)
}
if res.Stream != nil {
// handle response
}
}
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a retry.Config
object to the call by using the WithRetries
option:
package main
import (
"context"
livepeergo "github.com/livepeer/livepeer-go"
"github.com/livepeer/livepeer-go/models/components"
"github.com/livepeer/livepeer-go/retry"
"log"
"models/operations"
)
func main() {
s := livepeergo.New(
livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
)
ctx := context.Background()
res, err := s.Stream.Create(ctx, components.NewStreamPayload{
Name: "test_stream",
Pull: &components.Pull{
Source: "https://myservice.com/live/stream.flv",
Headers: map[string]string{
"Authorization": "Bearer 123",
},
Location: &components.Location{
Lat: 39.739,
Lon: -104.988,
},
},
PlaybackPolicy: &components.PlaybackPolicy{
Type: components.TypeWebhook,
WebhookID: livepeergo.String("1bde4o2i6xycudoy"),
WebhookContext: map[string]any{
"streamerId": "my-custom-id",
},
RefreshInterval: livepeergo.Float64(600),
},
Profiles: []components.FfmpegProfile{
components.FfmpegProfile{
Width: 1280,
Name: "720p",
Height: 720,
Bitrate: 3000000,
Fps: 30,
FpsDen: livepeergo.Int64(1),
Quality: livepeergo.Int64(23),
Gop: livepeergo.String("2"),
Profile: components.ProfileH264Baseline.ToPointer(),
},
},
Record: livepeergo.Bool(false),
RecordingSpec: &components.NewStreamPayloadRecordingSpec{
Profiles: []components.TranscodeProfile{
components.TranscodeProfile{
Width: livepeergo.Int64(1280),
Name: livepeergo.String("720p"),
Height: livepeergo.Int64(720),
Bitrate: 3000000,
Quality: livepeergo.Int64(23),
Fps: livepeergo.Int64(30),
FpsDen: livepeergo.Int64(1),
Gop: livepeergo.String("2"),
Profile: components.TranscodeProfileProfileH264Baseline.ToPointer(),
Encoder: components.TranscodeProfileEncoderH264.ToPointer(),
},
},
},
Multistream: &components.Multistream{
Targets: []components.Target{
components.Target{
Profile: "720p",
VideoOnly: livepeergo.Bool(false),
ID: livepeergo.String("PUSH123"),
Spec: &components.TargetSpec{
Name: livepeergo.String("My target"),
URL: "rtmps://live.my-service.tv/channel/secretKey",
},
},
},
},
}, operations.WithRetries(
retry.Config{
Strategy: "backoff",
Backoff: &retry.BackoffStrategy{
InitialInterval: 1,
MaxInterval: 50,
Exponent: 1.1,
MaxElapsedTime: 100,
},
RetryConnectionErrors: false,
}))
if err != nil {
log.Fatal(err)
}
if res.Stream != nil {
// handle response
}
}
If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig
option at SDK initialization:
package main
import (
"context"
livepeergo "github.com/livepeer/livepeer-go"
"github.com/livepeer/livepeer-go/models/components"
"github.com/livepeer/livepeer-go/retry"
"log"
)
func main() {
s := livepeergo.New(
livepeergo.WithRetryConfig(
retry.Config{
Strategy: "backoff",
Backoff: &retry.BackoffStrategy{
InitialInterval: 1,
MaxInterval: 50,
Exponent: 1.1,
MaxElapsedTime: 100,
},
RetryConnectionErrors: false,
}),
livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
)
ctx := context.Background()
res, err := s.Stream.Create(ctx, components.NewStreamPayload{
Name: "test_stream",
Pull: &components.Pull{
Source: "https://myservice.com/live/stream.flv",
Headers: map[string]string{
"Authorization": "Bearer 123",
},
Location: &components.Location{
Lat: 39.739,
Lon: -104.988,
},
},
PlaybackPolicy: &components.PlaybackPolicy{
Type: components.TypeWebhook,
WebhookID: livepeergo.String("1bde4o2i6xycudoy"),
WebhookContext: map[string]any{
"streamerId": "my-custom-id",
},
RefreshInterval: livepeergo.Float64(600),
},
Profiles: []components.FfmpegProfile{
components.FfmpegProfile{
Width: 1280,
Name: "720p",
Height: 720,
Bitrate: 3000000,
Fps: 30,
FpsDen: livepeergo.Int64(1),
Quality: livepeergo.Int64(23),
Gop: livepeergo.String("2"),
Profile: components.ProfileH264Baseline.ToPointer(),
},
},
Record: livepeergo.Bool(false),
RecordingSpec: &components.NewStreamPayloadRecordingSpec{
Profiles: []components.TranscodeProfile{
components.TranscodeProfile{
Width: livepeergo.Int64(1280),
Name: livepeergo.String("720p"),
Height: livepeergo.Int64(720),
Bitrate: 3000000,
Quality: livepeergo.Int64(23),
Fps: livepeergo.Int64(30),
FpsDen: livepeergo.Int64(1),
Gop: livepeergo.String("2"),
Profile: components.TranscodeProfileProfileH264Baseline.ToPointer(),
Encoder: components.TranscodeProfileEncoderH264.ToPointer(),
},
},
},
Multistream: &components.Multistream{
Targets: []components.Target{
components.Target{
Profile: "720p",
VideoOnly: livepeergo.Bool(false),
ID: livepeergo.String("PUSH123"),
Spec: &components.TargetSpec{
Name: livepeergo.String("My target"),
URL: "rtmps://live.my-service.tv/channel/secretKey",
},
},
},
},
})
if err != nil {
log.Fatal(err)
}
if res.Stream != nil {
// handle response
}
}