Code examples
Complete examples that request a Sandbox token and read customer information.
Protect your Voyced access from the first test
Your API credentials can access customer information and the functions allowed for that key. Treat the API secret like a password. Store it only in protected server-side settings or a trusted secret store.
- Never put an API key, API secret or bearer token in public code, browser JavaScript, screenshots, URLs or support messages.
- Keep Sandbox and Live credentials separate. Give each integration only the permissions it needs.
- Rotate the secret and disable the old key at once when exposure is suspected.
Recommended first test
Complete PHP Sandbox test
Download one file, add your Sandbox API key and secret, then test authentication, customer access, capabilities, numbers, balance and Follow-Me.
Use environment variables
Keep credentials outside the code. The examples read them from environment settings.
Required environment settings
Shell
export VOYCED_API_BASE_URL="https://sandbox.voycedconnect.eu/v1"
export VOYCED_API_KEY="YOUR_SANDBOX_API_KEY"
export VOYCED_API_SECRET="YOUR_SANDBOX_API_SECRET"Quick-start examples
cURL
: "${VOYCED_API_BASE_URL:=https://sandbox.voycedconnect.eu/v1}"
: "${VOYCED_API_KEY:?Set VOYCED_API_KEY}"
: "${VOYCED_API_SECRET:?Set VOYCED_API_SECRET}"
PAYLOAD=$(python3 - <<'PYJSON'
import json, os
print(json.dumps({"api_key": os.environ["VOYCED_API_KEY"], "api_secret": os.environ["VOYCED_API_SECRET"]}))
PYJSON
)
TOKEN_RESPONSE=$(curl --fail-with-body --silent --show-error \
--proto '=https' --tlsv1.2 --connect-timeout 5 --max-time 20 \
--request POST --url "$VOYCED_API_BASE_URL/auth/token" \
--header 'Accept: application/json' --header 'Content-Type: application/json' \
--data "$PAYLOAD")
TOKEN=$(printf '%s' "$TOKEN_RESPONSE" | python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["access_token"])')
curl --fail-with-body --silent --show-error \
--proto '=https' --tlsv1.2 --connect-timeout 5 --max-time 20 \
--request GET --url "$VOYCED_API_BASE_URL/customer" \
--header 'Accept: application/json' --header "Authorization: Bearer $TOKEN"
PHP
<?php
declare(strict_types=1);
$baseUrl = rtrim((string) (getenv('VOYCED_API_BASE_URL') ?: 'https://sandbox.voycedconnect.eu/v1'), '/');
$apiKey = (string) getenv('VOYCED_API_KEY');
$apiSecret = (string) getenv('VOYCED_API_SECRET');
if (strpos($baseUrl, 'https://') !== 0) {
throw new RuntimeException('VOYCED_API_BASE_URL must use HTTPS.');
}
if ($apiKey === '' || $apiSecret === '') {
throw new RuntimeException('Set VOYCED_API_KEY and VOYCED_API_SECRET.');
}
function voycedRequest(string $method, string $url, array $headers, ?array $json = null): array
{
$curl = curl_init($url);
if ($curl === false) throw new RuntimeException('Could not initialise cURL.');
$options = [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 20,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => $headers,
];
if ($json !== null) $options[CURLOPT_POSTFIELDS] = json_encode($json, JSON_THROW_ON_ERROR);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$status = (int) curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
if ($response === false) {
$message = curl_error($curl);
curl_close($curl);
throw new RuntimeException($message);
}
curl_close($curl);
$decoded = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
if ($status < 200 || $status >= 300) {
throw new RuntimeException((string) ($decoded['error']['message'] ?? 'Voyced API request failed.'));
}
return $decoded;
}
$tokenResponse = voycedRequest('POST', $baseUrl . '/auth/token', [
'Accept: application/json', 'Content-Type: application/json',
], ['api_key' => $apiKey, 'api_secret' => $apiSecret]);
$customer = voycedRequest('GET', $baseUrl . '/customer', [
'Accept: application/json', 'Authorization: Bearer ' . $tokenResponse['data']['access_token'],
]);
print_r($customer);
Node.js
const baseUrl = (process.env.VOYCED_API_BASE_URL || 'https://sandbox.voycedconnect.eu/v1').replace(/\/$/, '');
const apiKey = process.env.VOYCED_API_KEY;
const apiSecret = process.env.VOYCED_API_SECRET;
if (!apiKey || !apiSecret) throw new Error('Set VOYCED_API_KEY and VOYCED_API_SECRET.');
if (!baseUrl.startsWith('https://')) throw new Error('VOYCED_API_BASE_URL must use HTTPS.');
async function voyced(path, options = {}) {
const response = await fetch(`${baseUrl}${path}`, {
...options,
redirect: 'error',
signal: AbortSignal.timeout(20_000),
});
const result = await response.json();
if (!response.ok) throw new Error(result.error?.message || `Voyced API HTTP ${response.status}`);
return result;
}
const tokenResponse = await voyced('/auth/token', {
method: 'POST',
headers: {Accept: 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({api_key: apiKey, api_secret: apiSecret}),
});
const customer = await voyced('/customer', {
headers: {Accept: 'application/json', Authorization: `Bearer ${tokenResponse.data.access_token}`},
});
console.log(JSON.stringify(customer, null, 2));
Python
import os
import requests
base_url = os.getenv("VOYCED_API_BASE_URL", "https://sandbox.voycedconnect.eu/v1").rstrip("/")
if not base_url.startswith("https://"):
raise RuntimeError("VOYCED_API_BASE_URL must use HTTPS.")
api_key = os.environ["VOYCED_API_KEY"]
api_secret = os.environ["VOYCED_API_SECRET"]
session = requests.Session()
session.headers["Accept"] = "application/json"
token_response = session.post(
f"{base_url}/auth/token",
json={"api_key": api_key, "api_secret": api_secret},
timeout=20,
allow_redirects=False,
)
token_response.raise_for_status()
token = token_response.json()["data"]["access_token"]
customer_response = session.get(
f"{base_url}/customer",
headers={"Authorization": f"Bearer {token}"},
timeout=20,
allow_redirects=False,
)
customer_response.raise_for_status()
print(customer_response.json())
Download the files
Shell quick startToken and customer request
DownloadPHP quick startPHP cURL example inside a ZIP
Download ZIPNode.js quick startBuilt-in fetch example
DownloadPython quick startRequests example
DownloadMove the same code to Live
After Voyced enables Live access, change only the protected environment settings. Do not change the endpoint paths.
Live settings
VOYCED_API_BASE_URL=https://api.voycedconnect.eu/v1
VOYCED_API_KEY=YOUR_LIVE_API_KEY
VOYCED_API_SECRET=YOUR_LIVE_API_SECRET