Class: LiveKit::LiveKitAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/livekit/livekit_api.rb

Overview

A single entry point to every LiveKit server API, exposing each service through a reader.

Examples:

api = LiveKit::LiveKitAPI.new("https://my.livekit.host", api_key: k, api_secret: s)
api.room.create_room("my-room")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, api_key: nil, api_secret: nil, token: nil, failover: true) ⇒ LiveKitAPI

Authenticate with either an API key and secret (recommended for backend use), or a pre-signed token (for client-side use, where the API secret must not be exposed). Any omitted value falls back to its environment variable: LIVEKIT_URL, LIVEKIT_TOKEN, LIVEKIT_API_KEY, LIVEKIT_API_SECRET.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/livekit/livekit_api.rb', line 36

def initialize(url = nil, api_key: nil, api_secret: nil, token: nil, failover: true)
  url ||= ENV["LIVEKIT_URL"]

  # Only fall back to environment credentials when none were provided
  # explicitly, so an ambient LIVEKIT_TOKEN can't silently override an
  # explicit api_key/secret (or vice versa).
  if token.nil? && api_key.nil? && api_secret.nil?
    token = ENV["LIVEKIT_TOKEN"]
  end
  if token.nil? && api_key.nil? && api_secret.nil?
    api_key = ENV["LIVEKIT_API_KEY"]
    api_secret = ENV["LIVEKIT_API_SECRET"]
  end

  raise ArgumentError, "url is required (pass it or set LIVEKIT_URL)" if url.nil? || url.empty?
  if token.nil? && (api_key.nil? || api_secret.nil?)
    raise ArgumentError, "either a token, or api_key and api_secret, are required"
  end

  # Share one Faraday connection (and its adapter/middleware) across every
  # service instead of letting each open its own.
  connection = LiveKit::Failover.connection(url, failover)
  opts = { api_key: api_key, api_secret: api_secret, token: token, failover: failover, connection: connection }
  @room = RoomServiceClient.new(url, **opts)
  @egress = EgressServiceClient.new(url, **opts)
  @ingress = IngressServiceClient.new(url, **opts)
  @sip = SIPServiceClient.new(url, **opts)
  @agent_dispatch = AgentDispatchServiceClient.new(url, **opts)
  @connector = ConnectorServiceClient.new(url, **opts)
end

Instance Attribute Details

#agent_dispatchAgentDispatchServiceClient (readonly)



28
29
30
# File 'lib/livekit/livekit_api.rb', line 28

def agent_dispatch
  @agent_dispatch
end

#connectorConnectorServiceClient (readonly)



30
31
32
# File 'lib/livekit/livekit_api.rb', line 30

def connector
  @connector
end

#egressEgressServiceClient (readonly)

Returns:



22
23
24
# File 'lib/livekit/livekit_api.rb', line 22

def egress
  @egress
end

#ingressIngressServiceClient (readonly)



24
25
26
# File 'lib/livekit/livekit_api.rb', line 24

def ingress
  @ingress
end

#roomRoomServiceClient (readonly)

Returns:



20
21
22
# File 'lib/livekit/livekit_api.rb', line 20

def room
  @room
end

#sipSIPServiceClient (readonly)

Returns:



26
27
28
# File 'lib/livekit/livekit_api.rb', line 26

def sip
  @sip
end