initApi method

void initApi({
  1. String baseUrl = UTDApiClient.defaultBaseUrl,
  2. String engineBaseUrl = UTDApiClient.defaultEngineBaseUrl,
  3. String? appId,
  4. String? appKey,
})

Initializes the UTD Stream Engine API clients.

Must be called before connect if you want to use the new backend APIs. The engine is split across two hosts (see UTDApiClient): baseUrl — token/auth host (POST /api/v1/token). Defaults to UTDApiClient.defaultBaseUrl (https://udt-stream.com). engineBaseUrl — in-room ops host (seats, speakers, roles, bans, participants). Defaults to UTDApiClient.defaultEngineBaseUrl (https://engine.udt-stream.com). appId — the App ID (X-App-Id header). appKey — the publishable app key (no backend required): the kit mints tokens directly from the engine with X-App-Key and uses the per-user bearer it returns for in-room actions. The server secret never ships. This is the only supported credential path.

Implementation

void initApi({
  String baseUrl = UTDApiClient.defaultBaseUrl,
  String engineBaseUrl = UTDApiClient.defaultEngineBaseUrl,
  String? appId,
  String? appKey,
}) {
  _appId = appId;
  _appKey = appKey;
  // No-backend (app_key) mode: only the token client carries X-App-Key (for minting); in-room
  // calls authenticate with the per-user bearer applied after the first generateToken.
  // Token minting goes to the auth host; everything in-room goes to the engine (grey-cloud) host.
  _tokenClient = UTDApiClient(
    baseUrl: baseUrl,
    appId: appId,
    appKey: appKey,
  );
  _engineClient = UTDApiClient(
    baseUrl: engineBaseUrl,
    appId: appId,
  );
  _tokenApi = UTDTokenApi(_tokenClient!);
  _stageApi = UTDStageApi(_engineClient!);
  _banApi = UTDBanApi(_engineClient!);
  _roleApi = UTDRoleApi(_engineClient!);
  _participantApi = UTDParticipantApi(_engineClient!);
  // PK battles ride the engine (in-room) client on the per-user bearer, same as
  // stage/ban/role. The controller INSTANCE is kept across re-inits so a live
  // battle's dual-publish survives (restore-from-minimize), but it must be
  // re-pointed at the fresh client: initApi can run more than once (widget
  // defaults first, then the app's real hosts in onControllerReady) and a
  // stale client would send every PK call to the wrong host.
  _pkApi = UTDPkApi(_engineClient!);
  if (pkController == null) {
    pkController = UTDPkController(this, _pkApi!);
  } else {
    pkController!.updateApi(_pkApi!);
  }
  // Re-apply the per-user bearer to the freshly built clients so secure-mode credentials
  // survive a re-init. The reuse path (restore-from-minimize) calls initApi() again without
  // re-minting a token; without this the new clients would carry neither secret nor bearer and
  // every in-room/moderation call would go out unauthenticated.
  if (_lastUserToken != null) {
    _applyUserToken(_lastUserToken!);
  }
  debugPrint('[UTDRoomController] API initialized: token=$baseUrl, engine=$engineBaseUrl'
      '${appId != null ? ', appId: $appId' : ''}'
      '${appKey != null ? ', mode: appKey' : ''}');
}