@@ -29,6 +29,7 @@ def __init__(
2929 api_secret : Optional [str ] = None ,
3030 * ,
3131 timeout : aiohttp .ClientTimeout = aiohttp .ClientTimeout (total = 60 ), # 60 seconds
32+ session : aiohttp .ClientSession | None = 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,9 @@ 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+ self ._custom_session = True if session is None else False
54+ self ._session = session or aiohttp .ClientSession (timeout = timeout )
55+
5256 self ._room = RoomService (self ._session , url , api_key , api_secret )
5357 self ._ingress = IngressService (self ._session , url , api_key , api_secret )
5458 self ._egress = EgressService (self ._session , url , api_key , api_secret )
@@ -84,7 +88,9 @@ async def aclose(self):
8488 """Close the API client
8589
8690 Call this before your application exits or when the API client is no longer needed."""
87- await self ._session .close ()
91+ # we do not close custom sessions, that's up to the caller
92+ if not self ._custom_session :
93+ await self ._session .close ()
8894
8995 async def __aenter__ (self ):
9096 """@private
0 commit comments