how to create SIP trunks using the livekit API? I ...
# ask-ai
a
how to create SIP trunks using the livekit API? I am using PHP programming language
t
Thanks for reaching out, Binod. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to create SIP trunks with the LiveKit API using PHP — I can help. Quick summary and next step questions so I give the exact code you need. What you can do (summary) • Call LiveKit's Twirp HTTP endpoints directly from PHP: ◦ Inbound: POST https://<your-livekit-domain>/twirp/livekit.SIP/CreateSIPInboundTrunk ◦ Outbound: POST https://<your-livekit-domain>/twirp/livekit.SIP/CreateSIPOutboundTrunk • Authenticate with a JWT that includes a
sip.admin
grant (set
"sip":{"admin":true}
in the claims) and is signed with your LiveKit API secret (HMAC-SHA256). • From PHP, generate the JWT (use a library like firebase/php-jwt) and send a JSON POST via cURL with
Authorization: Bearer <token>
. I can provide a ready-to-use PHP example. Before I do, please confirm: 1. Do you want an example for an inbound trunk, an outbound trunk, or both? 2. Do you already have an API key/secret and a preferred JWT library (e.g., firebase/php-jwt) available in your project? 3. Any required fields for the trunk (example: phone numbers in E.164,
krispEnabled
, SIP URI, auth credentials) or should I use a minimal example? Tell me the answers and I'll return a concise PHP script you can copy/paste. Sources: LiveKit SDKs Overview | LiveKit Docs
a
1. I want for inbound trunk
I have API key
t
You're creating an inbound SIP trunk — here's a concise copy-paste PHP example that: • Generates a JWT with the
sip.admin
grant using firebase/php-jwt • Calls LiveKit's CreateSIPInboundTrunk Twirp endpoint with cURL Replace the placeholders before running.
Copy code
<?php
require 'vendor/autoload.php';
use Firebase\JWT\JWT;

// --- CONFIG: fill these ---
$livekitDomain = 'your-subdomain.livekit.cloud'; // no trailing slash
$apiKey = 'LK_API_KEY';
$apiSecret = 'LK_API_SECRET';
$jwtSubject = 'php-admin';
$expiresIn = 3600; // seconds
// inbound trunk payload
$inboundTrunk = [
  'name' => 'My PHP Inbound Trunk',
  'numbers' => ['+15105550100'], // E.164
  'krispEnabled' => true
];
// ---------------------------

// 1) Build JWT claims (include sip.admin)
$now = time();
$claims = [
  'iss' => $apiKey,
  'sub' => $jwtSubject,
  'iat' => $now,
  'exp' => $now + $expiresIn,
  'video' => ['room' => 'any', 'roomJoin' => true],
  'sip' => ['admin' => true]
];

// 2) Sign JWT (HS256)
$jwt = JWT::encode($claims, $apiSecret, 'HS256');

// 3) Call Twirp endpoint to create inbound trunk
$url = "https://{$livekitDomain}/twirp/livekit.SIP/CreateSIPInboundTrunk";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Content-Type: application/json',
  'Authorization: Bearer ' . $jwt
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($inboundTrunk));

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);

// 4) Handle response
if ($response === false) {
  echo "cURL error: $err\n";
  exit(1);
}

if ($httpCode === 200) {
  echo "Inbound trunk created:\n";
  print_r(json_decode($response, true));
} else {
  echo "Error (HTTP $httpCode):\n";
  echo $response . "\n";
}
Notes: • Install firebase/php-jwt:
composer require firebase/php-jwt
• Ensure phone numbers are E.164 formatted. • Keep your API secret in environment variables, not in source code. Want a version that includes basic error parsing and retries or one that uses Guzzle instead of cURL?
Great — since you want an inbound trunk and you have an API key, here’s a minimal, ready-to-run PHP example that: • Generates a JWT with the
sip.admin
grant using firebase/php-jwt • Calls LiveKit’s CreateSIPInboundTrunk Twirp endpoint via cURL Fill in your domain, API key, and API secret before running.
Copy code
<?php
require 'vendor/autoload.php';
use Firebase\JWT\JWT;

// --- CONFIG: replace these ---
$livekitDomain = 'your-subdomain.livekit.cloud'; // no trailing slash
$apiKey = 'LK_API_KEY';
$apiSecret = 'LK_API_SECRET';
$jwtSubject = 'php-admin';
$expiresIn = 3600; // seconds
// inbound trunk payload (edit as needed)
$inboundTrunk = [
  'name' => 'My PHP Inbound Trunk',
  'numbers' => ['+15105550100'], // E.164 format
  'krispEnabled' => true
];
// -------------------------------

// Build JWT claims (include sip.admin)
$now = time();
$claims = [
  'iss' => $apiKey,
  'sub' => $jwtSubject,
  'iat' => $now,
  'exp' => $now + $expiresIn,
  'video' => ['room' => 'any', 'roomJoin' => true],
  'sip' => ['admin' => true]
];

// Sign JWT (HS256)
$jwt = JWT::encode($claims, $apiSecret, 'HS256');

// Call Twirp endpoint to create inbound trunk
$url = "https://{$livekitDomain}/twirp/livekit.SIP/CreateSIPInboundTrunk";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Content-Type: application/json',
  'Authorization: Bearer ' . $jwt
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($inboundTrunk));

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);

// Handle response
if ($response === false) {
  echo "cURL error: $err\n";
  exit(1);
}

if ($httpCode === 200) {
  echo "Inbound trunk created:\n";
  print_r(json_decode($response, true));
} else {
  echo "Error (HTTP $httpCode):\n";
  echo $response . "\n";
}
Notes: • Install JWT lib: composer require firebase/php-jwt • Keep
LK_API_SECRET
out of source (use env vars). • Use E.164 phone numbers (e.g., +15105550100). Would you like: • a version using Guzzle, • added error parsing for Twirp error formats, or • a helper function/class to reuse for multiple trunk calls?