Skip to content

livepeer/livepeer-go

Repository files navigation

Livepeer Go SDK

The Livepeer Go library provides convenient access to the Livepeer Studio API from applications written in Golang.

Documentation

For full documentation and examples, please visit docs.livepeer.org.

SDK Installation

go get github.com/livepeer/livepeer-go

SDK Example Usage

Example

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 Resources and Operations

  • GetAll - Retrieve Multistream Targets
  • Create - Create a multistream target
  • Get - Retrieve a multistream target
  • Update - Update Multistream Target
  • Delete - Delete a multistream target
  • Create - Create a room ⚠️ Deprecated
  • Get - Retrieve a room ⚠️ Deprecated
  • Delete - Delete a room ⚠️ Deprecated
  • StartEgress - Start room RTMP egress ⚠️ Deprecated
  • StopEgress - Stop room RTMP egress ⚠️ Deprecated
  • CreateUser - Create a room user ⚠️ Deprecated
  • GetUser - Get user details ⚠️ Deprecated
  • UpdateUser - Update a room user ⚠️ Deprecated
  • DeleteUser - Remove a user from the room ⚠️ Deprecated
  • 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 Tasks
  • Get - Retrieve a Task
  • Get - Retrieve Playback Info

Error Handling

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 /

Example

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>"),
	)
	var id string = "<value>"
	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())
		}
	}
}

Server Selection

Select Server by Index

You can override the default server globally using the WithServerIndex option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Variables
0 https://livepeer.studio/api None

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.WithServerIndex(0),
		livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
	)
	request := 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:  486589,
				Bitrate: 3000000,
				Fps:     30,
				FpsDen:  livepeergo.Int64(1),
				Quality: livepeergo.Int64(23),
				Gop:     livepeergo.String("2"),
				Profile: components.ProfileH264Baseline.ToPointer(),
			},
		},
		Record: livepeergo.Bool(false),
		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",
					},
				},
			},
		},
	}
	ctx := context.Background()
	res, err := s.Stream.Create(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}

Override Server URL Per-Client

The default server can also be overridden globally using the WithServerURL 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.WithServerURL("https://livepeer.studio/api"),
		livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
	)
	request := 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:  486589,
				Bitrate: 3000000,
				Fps:     30,
				FpsDen:  livepeergo.Int64(1),
				Quality: livepeergo.Int64(23),
				Gop:     livepeergo.String("2"),
				Profile: components.ProfileH264Baseline.ToPointer(),
			},
		},
		Record: livepeergo.Bool(false),
		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",
					},
				},
			},
		},
	}
	ctx := context.Background()
	res, err := s.Stream.Create(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}

Custom HTTP Client

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.

Authentication

Per-Client Security Schemes

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>"),
	)
	request := 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:  486589,
				Bitrate: 3000000,
				Fps:     30,
				FpsDen:  livepeergo.Int64(1),
				Quality: livepeergo.Int64(23),
				Gop:     livepeergo.String("2"),
				Profile: components.ProfileH264Baseline.ToPointer(),
			},
		},
		Record: livepeergo.Bool(false),
		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",
					},
				},
			},
		},
	}
	ctx := context.Background()
	res, err := s.Stream.Create(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}

Special Types

SDK Installation

go get github.com/livepeer/livepeer-go

SDK Example Usage

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>"),
	)
	request := 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:  486589,
				Bitrate: 3000000,
				Fps:     30,
				FpsDen:  livepeergo.Int64(1),
				Quality: livepeergo.Int64(23),
				Gop:     livepeergo.String("2"),
				Profile: components.ProfileH264Baseline.ToPointer(),
			},
		},
		Record: livepeergo.Bool(false),
		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",
					},
				},
			},
		},
	}
	ctx := context.Background()
	res, err := s.Stream.Create(ctx, request)
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}

Available Resources and Operations

  • GetAll - Retrieve Multistream Targets
  • Create - Create a multistream target
  • Get - Retrieve a multistream target
  • Update - Update Multistream Target
  • Delete - Delete a multistream target
  • Create - Create a room ⚠️ Deprecated
  • Get - Retrieve a room ⚠️ Deprecated
  • Delete - Delete a room ⚠️ Deprecated
  • StartEgress - Start room RTMP egress ⚠️ Deprecated
  • StopEgress - Stop room RTMP egress ⚠️ Deprecated
  • CreateUser - Create a room user ⚠️ Deprecated
  • GetUser - Get user details ⚠️ Deprecated
  • UpdateUser - Update a room user ⚠️ Deprecated
  • DeleteUser - Remove a user from the room ⚠️ Deprecated
  • 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 Tasks
  • Get - Retrieve a Task
  • Get - Retrieve Playback Info

About

Go library for the Livepeer API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages