Developers
Create a UPCS Embed Session in PHP
PHP example using cURL to create a UPCS embed session on your server, parse the session_token and handle error responses safely.
Updated 14 October 2026 · 2 min read
A plain PHP script can create an embed session with the cURL extension. Wrap the call in a function, return only the token to the browser, and keep the key in the environment.
The function
<?php
function createUpcsEmbedSession(string $origin, ?int $ttl = null): array
{
$payload = ['origin' => $origin];
if ($ttl !== null) {
$payload['ttl'] = $ttl; // 60-3600 seconds
}
$ch = curl_init('https://api.upcs.io/api/v1/embed/sessions');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'x-api-key: ' . getenv('UPCS_API_KEY'),
'Content-Type: application/json',
'Accept: application/json',
],
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$raw = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode((string) $raw, true) ?: [];
if ($status >= 400 || ($data['success'] ?? true) === false) {
throw new RuntimeException(
($data['code'] ?? 'error') . ': ' . ($data['message'] ?? 'UPCS request failed'),
$status
);
}
return $data; // session_token, expires_at, embed_url
}
An endpoint for your page
<?php
header('Content-Type: application/json');
try {
$session = createUpcsEmbedSession('https://shop.example.com');
echo json_encode(['session_token' => $session['session_token']]);
} catch (RuntimeException $e) {
error_log($e->getMessage());
http_response_code(502);
echo json_encode(['error' => 'Map is temporarily unavailable']);
}
Your page then fetches this endpoint and passes the token to UPCS.mount; the full flow is in embedding the map on a website.
Notes
- Set a timeout so a slow upstream call cannot hang your page.
- Use a fixed origin from configuration rather than a request header.
- If you receive
429, slow down; see rate limits and backoff. - If you use Laravel, the Laravel example uses the framework's HTTP client instead.
Error meanings are listed in the error codes reference.
Frequently asked questions
Do I need Composer or a library?
No. PHP's built-in cURL extension is enough. UPCS has no official PHP SDK.
Where should the API key live?
In an environment variable or a configuration value outside the web root, never in a file served to visitors.
Related reading
Create a UPCS Embed Session in PythonPython example using the requests library to create a UPCS embed session on your server and return only the session token to the browser.UPCS.mount Options ExplainedReference for every UPCS.mount option: sessionToken, height, center, zoom, style, onSelect, onError and onReady, plus the returned handle.Handling the onSelect ResponseWhat the UPCS embed onSelect callback returns, which fields to store, and a safe pattern for validating and saving the selected location.Addresses Without House NumbersWhat do you write when a home has no number? Learn how house names, plot identifiers and landmarks fill in and how to keep mail on track.
