OpenID:User
An OpenID:User token lets your application call a Wildix API on behalf of a regular PBX user, with that user's extension and web password. This page shows how to get the token, how to use it with the TypeScript SDK and with cURL, and how to keep it fresh.
Table of Contents
- What is an OpenID:User token?
- Prerequisites
- How to get a token
- TypeScript SDK
- cURL Example
- Token lifetime and caching
- Troubleshooting
- Best Practices
What is an OpenID:User token?
An OpenID:User token is a signed JWT issued by the Wildix auth service for one PBX user and one API. It is a good fit when:
- Your integration acts as a person: every request is attributed to the user's extension, so messages, call records and billing records show who did it.
- You cannot use a Company API Key: the API you call does not accept keys, or no root admin is available to create one.
The token carries the user's company and extension (auth:companyId, auth:extension) and lives about 11 hours. It is accepted only by the API it was issued for: the aud (audience) you request must be that API's server URL. The API reference names it under the OpenID:User authorization of each API; for example, the Classound API uses https://classound-p2p.wildix.com.
Tip: when the API supports it, a Company API Key is simpler: no exchange, no refresh, and it lives up to a year.
Prerequisites
- PBX URL, for example
https://example.wildixin.com - A regular PBX user with its extension (or login) and web password. Use a normal user, not the PBX admin: Basic authorization is disabled for the admin by default. A dedicated user created for the integration is the cleanest choice.
- Node.js 18 or higher for the TypeScript examples
How to get a token
Two calls turn the user's credentials into a token for the API:
- Personal token from the PBX.
GET <PBX URL>/api/v1/personal/token/?service=auth.wildix.comwith Basic authorization (extension and password). The answer holdsresult.token, which lives about 30 seconds. - OpenID:User token from the auth service.
POST https://auth.wildix.com/api/v2/OpenId/servicewithAuthorization: Bearer <result.token>and the body{"aud": "<API server URL>", "scope": "post"}. The answer holdsresult.id_token.
Then call the API with:
Authorization: Bearer <id_token>
⚠️ Important: send
audas the plain URL, for examplehttps://classound-p2p.wildix.com. The auth service adds theUSER::prefix itself. If you sendUSER::https://..., the token comes back with a doubled prefix and the API answers401.
TypeScript SDK
OpenIdUserProvider from @wildix/auth-utils makes both calls and caches the token. The example below uses it with the Classound API client.
npm install @wildix/auth-utils @wildix/classound-client
Get a token
import { OpenIdUserProvider } from '@wildix/auth-utils';
const userToken = new OpenIdUserProvider({
pbxUri: 'https://example.wildixin.com', // with https://
user: process.env.PBX_USER!, // extension or login
pass: process.env.PBX_PASSWORD!, // web password, from your secret store
aud: 'https://classound-p2p.wildix.com', // server URL of the API you call
scope: 'post',
});
const idToken = await userToken.token(); // cached until it expires
Call an API
Pass the token to the client as a function that returns { token }. The client calls it before each request, and the provider answers from its cache.
import { ClassoundClient, ListWhatsAppTemplatesA2SCommand } from '@wildix/classound-client';
const classound = new ClassoundClient({
env: 'prod',
token: async () => ({ token: await userToken.token() }),
});
try {
const templates = await classound.send(new ListWhatsAppTemplatesA2SCommand({ did: '+441234567890' }));
console.log(templates.data.map((t) => t.name));
} catch (error: any) {
// The service's reason is in `description`, for example "Number does not belong to company"
console.error(error.$metadata?.httpStatusCode, error.description ?? error.message);
}
⚠️ Note:
new ClassoundClient({ token: userToken })does not work: the Classound client expects a function or a{ token }object, and fails withInvalid character in header content ["Authorization"].
cURL Example
PBX=https://example.wildixin.com
USER_EXT=1001
USER_PASS='...' # read it from your secret store, never hard-code it
# 1. Personal token from the PBX
PERSONAL=$(curl -sS "$PBX/api/v1/personal/token/?service=auth.wildix.com" \
-u "$USER_EXT:$USER_PASS" | jq -r .result.token)
# 2. OpenID:User token for the API (here: Classound)
TOKEN=$(curl -sS -X POST "https://auth.wildix.com/api/v2/OpenId/service" \
-H "Authorization: Bearer $PERSONAL" \
-H "Content-Type: application/json" \
-d '{"aud":"https://classound-p2p.wildix.com","scope":"post"}' | jq -r .result.id_token)
# 3. Call the API
curl -sS "https://classound-p2p.wildix.com/v1/a2s/whatsapp/templates?did=%2B441234567890" \
-H "Authorization: Bearer $TOKEN"
Token lifetime and caching
- The token lives about 11 hours. Get it once and reuse it for every call; do not request a new token per request.
OpenIdUserProvidercaches the token in memory and gets a new one 10 seconds before it expires. Create one provider per user and API, and keep it for the life of your process.- Without the SDK, cache the token yourself and refresh it a few minutes before its
expclaim. - If a call answers
401, get a new token once and retry that call once.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
Personal token from PBX not received (SDK), 401 at step 1 | Wrong extension or password, or the PBX admin user | Check the credentials; use a regular user |
TypeError: Failed to parse URL (SDK) | pbxUri without https:// | Write the full URL, https://example.wildixin.com |
401 from the API with a fresh token | aud sent with the USER:: prefix, or the token was issued for another API | Send aud as the plain server URL of the API you call |
401 from the API after hours of work | The token expired | Get a new token and retry once |
Best Practices
- Use a dedicated PBX user for each integration: its password is used only by your system, and its actions are easy to find.
- Keep the password out of code: read it from environment variables or a secret store.
- Never log tokens or passwords.
- Reuse the token until it expires instead of requesting a new one.