@@ -28,7 +28,8 @@ def __init__(
2828 api_key : Optional [str ] = None ,
2929 api_secret : Optional [str ] = None ,
3030 * ,
31- timeout : aiohttp .ClientTimeout = aiohttp .ClientTimeout (total = 60 ), # 60 seconds
31+ timeout : Optional [aiohttp .ClientTimeout ] = None ,
32+ session : Optional [aiohttp .ClientSession ] = None ,
3233 ):
3334 """Create a new LiveKitAPI instance.
3435
@@ -37,6 +38,7 @@ def __init__(
3738 api_key: API key (read from `LIVEKIT_API_KEY` environment variable if not provided)
3839 api_secret: API secret (read from `LIVEKIT_API_SECRET` environment variable if not provided)
3940 timeout: Request timeout (default: 60 seconds)
41+ session: aiohttp.ClientSession instance to use for requests, if not provided, a new one will be created
4042 """
4143 url = url or os .getenv ("LIVEKIT_URL" )
4244 api_key = api_key or os .getenv ("LIVEKIT_API_KEY" )
@@ -48,7 +50,12 @@ def __init__(
4850 if not api_key or not api_secret :
4951 raise ValueError ("api_key and api_secret must be set" )
5052
51- self ._session = aiohttp .ClientSession (timeout = timeout )
53+ if not timeout :
54+ timeout = aiohttp .ClientTimeout (total = 60 )
55+
56+ self ._custom_session = True if session is None else False
57+ self ._session = session or aiohttp .ClientSession (timeout = timeout )
58+
5259 self ._room = RoomService (self ._session , url , api_key , api_secret )
5360 self ._ingress = IngressService (self ._session , url , api_key , api_secret )
5461 self ._egress = EgressService (self ._session , url , api_key , api_secret )
@@ -84,7 +91,9 @@ async def aclose(self):
8491 """Close the API client
8592
8693 Call this before your application exits or when the API client is no longer needed."""
87- await self ._session .close ()
94+ # we do not close custom sessions, that's up to the caller
95+ if not self ._custom_session :
96+ await self ._session .close ()
8897
8998 async def __aenter__ (self ):
9099 """@private
0 commit comments