To connect client applications (Web, iOS, Android, Desktop) to Slyber Turn Server, you need to pass a list of server URIs along with temporary username/password credentials to the WebRTC RTCPeerConnection. This can be done in two ways: via the built-in HTTP API.
Fetching Credentials via the HTTP API
Request: GET /api/turn-credentials
URL Parameters (Query Parameters):
user_id(optional) - string user identifier for analytics and logs (if omitted,anonymous_<nanoseconds>is generated).ttl(optional) - desired token lifetime in seconds (without theX-Turn-Secretheader, this value cannot exceedCREDENTIAL_TTL_SECfrom the server configuration).
Headers:
X-Turn-Secret: <your_secret>orAuthorization: Bearer <your_secret>(required whenREQUIRE_SECRET=true).Origin: https://your-domain.com(validated when called directly from a browser).
Request Example (curl):
Bash
curl -s "http://127.0.0.1:8080/api/turn-credentials?user_id=user_42&ttl=3600" \
-H "X-Turn-Secret: your-super-secret-turn-key-12345"
Response Example (200 OK):
JSON
JSON
{
"username": "1761583600:user_42",
"password": "K8x9pL2mQ4vR7wT1yZ3aB5cD8eF=",
"ttl": 3600,
"uris": [
"stun:203.0.113.1:3478",
"turn:203.0.113.1:3478?transport=udp",
"turn:203.0.113.1:3478?transport=tcp",
"turns:rtc.example.com:5349?transport=tcp"
]
}
Note: Theturns:...URI is automatically added to the response if TLS is enabled on the server; furthermore, if a certificate file and a customREALM(other thanturn.local) are configured, the domain name fromREALMis automatically substituted into theturns:URI.
Browser Connection Example (JavaScript / WebRTC):
JavaScript
async function createPeerConnection(userId) {
// 1. Fetch temporary TURN credentials
const res = await fetch(`https://turn.example.com:8080/api/turn-credentials?user_id=${encodeURIComponent(userId)}`);
if (!res.ok) throw new Error('Failed to fetch TURN credentials');
const turnConfig = await res.json();
// 2. Pass the received URIs, username, and password to RTCPeerConnection
const pc = new RTCPeerConnection({
iceServers: [
{
urls: turnConfig.uris,
username: turnConfig.username,
credential: turnConfig.password
}
]
});
return pc;
}