SimliClient is awesome on it’s own. However, it uses the API key directly on client-side which isn’t so great for security as someone with a little bit of knowledge can get your API key. With SimliClient 1.2.7+, you can handle auth on your own backend by making the authentication request there and passing the session_token and ICE Config back to the client.
This is a really simple backend with one goal, proxying /startAudioToVideoSession and /getIceServer to Simli servers and keeping the API key safe on your backend. This sample backend will now have any auth on it’s own as there’s no one good solution for that and you can pick whatever you like for your auth!For this example, we’re using FastAPI with python but as we mentioned, you can use anything you like.
Copy
import osfrom dotenv import load_dotenvfrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModelimport httpx# Load environment variables from the .env fileload_dotenv()SIMLI_API_KEY = os.getenv("SIMLI_API_KEY")if not SIMLI_API_KEY: raise ValueError("SIMLI_API_KEY is not set in the environment")# External API endpointsEXTERNAL_AUDIO_TO_VIDEO_URL = "https://api.simli.ai/startAudioToVideoSession"EXTERNAL_ICE_SERVER_URL = "https://api.simli.ai/getIceServer"# Request model for the audio-to-video session.# Note: The apiKey is no longer expected from the client.class StartAudioToVideoSessionRequest(BaseModel): faceId: str handleSilence: bool = True maxSessionLength: int = 3600 maxIdleTime: int = 300app = FastAPI()@app.post("/myAudioToVideoSession")async def my_audio_to_video_session(request: StartAudioToVideoSessionRequest): """ Proxy endpoint that forwards a POST request to the external startAudioToVideoSession API with the API key injected from the .env file. """ # Convert the incoming request to a dict and inject the API key. payload = request.dict() payload["apiKey"] = SIMLI_API_KEY async with httpx.AsyncClient() as client: response = await client.post(EXTERNAL_AUDIO_TO_VIDEO_URL, json=payload) return response.json()@app.post("/myIceServer")async def my_ice_server(): """ Proxy endpoint that forwards a POST request to the external getIceServer API with the API key injected from the .env file. """ # Build the payload using the API key from the environment. payload = {"apiKey": SIMLI_API_KEY} async with httpx.AsyncClient() as client: response = await client.post(EXTERNAL_ICE_SERVER_URL, json=payload) return response.json()# To run the app, use:# uvicorn proxy:app --reloadif __name__ == "__main__": import uvicorn uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
We can make a call to our backend (which we’re assuming is on 127.0.0.1:8000 for now), get the session token and IceServers config and give them to the Simli-Client instance