Contact Identity Sync and Token Update
Enterprise systems sync contact identity and update Token credentials via setup-contact-credentials when creating users and on each login
After embedding Web Chat (including MaiGPT mode) into their product, enterprise systems need to let the AI know "who is asking": so the AI can trace back the user's conversation history and access the enterprise system's API with that user's permissions when calling MCP tools.
The setup-contact-credentials API accomplishes two things in a single call:
Create or update the mapping between "enterprise system account ↔ MaiAgent Contact"
Write the user's Access Token to Contact MCP Credentials, enabling the AI assistant to call tools on the user's behalf
Choosing Between Frontend Auth and Backend Integration
This API also has a frontend version: the embed SDK's auth configuration (see Web Chat Embedding and SDK) internally has the chat widget automatically call the same API. Both paths create the same set of contacts (the same sourceId maps to the same person); the only difference is who makes the call:
Caller
User's browser
Your backend server
Integration cost
Low: add an auth object to the config
Medium: add one API call to the login flow + store contactId
Token confidentiality
mcpCredentials appears on the frontend page; users can see their own token in the source code
Token never passes through the frontend
Best for
Only need to identify users and preserve cross-device conversations
Need to bind MCP credentials so the AI can call APIs with user permissions
1. When to Call
Creating a new user
Call this API during the account creation flow and store the returned contactId in the member data table
Each login
After login succeeds and an Access Token is generated, call this API before returning the result to the frontend, passing in the new Token (the same sourceId will auto-update; no duplicates are created)
Backfilling existing systems
Call this API for each existing user to backfill contact mappings; see Batch Backfill for Existing Users
Token expiry / refresh
Simply call this API again with the new Token
Logout
Call the SDK's signOut() on the frontend to return to anonymous; on the backend, delete the contact's tool credentials — see Logout and Credential Revocation
2. API Specification
Method
POST
URL
https://api.maiagent.ai/api/v1/web-chats/{webChatId}/setup-contact-credentials/
Authentication
No API Key required (public endpoint; only the WebChat ID is needed)
Content-Type
application/json
Rate limit
30 requests per IP per minute
Request Body
sourceId
✅
The unique identifier of the user in the enterprise system. Repeated calls with the same sourceId will update rather than create duplicates. Use non-guessable values (e.g., UUID); see the security note below
name
User display name, shown in the contact list in the MaiAgent dashboard; defaults to Anonymous if not provided. Only takes effect when the contact is first created; to update the name later, use Sync Contact Profile
mcpCredentials.toolId
The MCP tool ID to bind credentials to (omit the entire mcpCredentials object if not using MCP tools). Only MCP-type tools are accepted; passing an API tool ID will return 400. For API tool credentials, see Two Types of Tool Credentials
mcpCredentials.headers
HTTP headers to send to the MCP Server, enabling the AI to call tools as the user
embedOrigin
The Origin of the embedding page. When the WebChat has "allowed embed domains" configured, server-to-server calls (which lack a browser Origin header) need this field to pass validation
Response (200 OK)
The returned contactId must be stored in the enterprise system's member data, and passed in when initializing Web Chat on the frontend.
Both sourceId and contactId should be treated as identity credentials: This API is a public endpoint — anyone who knows the webChatId and a user's sourceId can retrieve their contactId; and with the contactId, they can converse as that user and view their conversation history. Therefore:
Use non-guessable, non-enumerable values for
sourceId(e.g., UUID) — do not use sequential IDs, emails, or phone numbersOnly output
contactIdto the user themselves on authenticated pages; do not include it in public page source codeConfigure the Allowed Embed Origins list for the Web Chat (provide the domain list to your MaiAgent integration contact) to restrict the origins that can call this API (see Web Chat Embed & SDK)
curl Example
3. Login Flow Integration Point
First Login
Subsequent Login / Token Refresh
Add a new field to the member data table:
maiagent_contact_id
UUID / VARCHAR(36), nullable
The corresponding MaiAgent Contact ID, stored after the first call
Token expiry handling: When a Token expires or is refreshed, simply call the same API again with the new Token. The same sourceId will automatically update the existing contact's credentials without creating duplicates.
Failures should not block login: If this API call fails, it should not prevent the user from logging into the enterprise system. Make the call asynchronous, log failures, and retry on the next login.
4. Batch Backfill for Existing Users
For large-scale backfills, use batch import instead: Both the dashboard and the API support importing via Excel in a single operation (up to 10,000 rows), with support for query_metadata and API tool credentials. See Contact Batch Import. The per-record approach below is suited for gradual backfilling through the login flow.
When integrating an existing system for the first time, call this API for each existing user to backfill contact mappings:
Pass each user's
sourceIdandname, and write the returnedcontactIdback to the member data tablesourceIdis idempotent: the backfill script can be safely re-run without creating duplicate contactsIf the user's Access Token is not available at the time of backfill, omit
mcpCredentialsand let the login flow add the credentials when the user next logs inMind the rate limit (30 requests per IP per minute); throttle accordingly for large-scale backfills
5. Sync Contact Profile (Optional)
To sync user information such as department, role, and service tier to MaiAgent (the AI assistant will automatically read this information and provide personalized responses), use the Contact API:
Method
PATCH
URL
https://api.maiagent.ai/api/v1/contacts/{contactId}/
Authentication
Authorization: Api-Key <<Your API Key>>
Content-Type
application/json
All fields are optional; PATCH only updates the fields provided — fields not included will not be cleared
The
metadatafield itself is a full replacement: the provided array will completely overwrite the old one, so include the full metadata arrayYou can call this on every login (to keep data current) or only when the user modifies their profile
6. Logout and Credential Revocation
There are two things to do at logout — frontend and backend — each with a different scope:
Frontend MaiAgent.auth.signOut()
Backend credential deletion (this section)
Effect
Chat window returns to anonymous state, clears contextData
Removes the contact's MCP/API tool credentials from MaiAgent
Tool credentials
Does not touch already-written credentials
Credentials are deleted immediately; the AI can no longer call tools as that user
When to call
When the user logs out on the page (see SDK API)
Called by your backend during the logout flow
If your system already revokes the Access Token on your own auth server at logout, the credentials stored by MaiAgent are effectively invalidated. Deleting credentials on the backend is a defense-in-depth measure — it is recommended to do both, to avoid stale Tokens lingering in the system.
Delete Credentials API
MCP tool
DELETE /api/v1/contacts/{contactId}/mcp-credentials/{credentialId}/
API tool
DELETE /api/v1/contacts/{contactId}/api-credentials/{credentialId}/
Authentication is Authorization: Api-Key <<Your API Key>> (same as the Contact API). A successful response returns 204 No Content:
After deletion, the contact itself, conversation history, and custom attributes are all preserved. When the user logs in again, the login flow's call to setup-contact-credentials will recreate the credentials (the same contact-tool combination is updated/recreated, not duplicated).
Retrieving the credentialId
setup-contact-credentials only returns a contactId and does not include the credential ID. To obtain the credentialId:
GET /api/v1/contacts/{contactId}/(Api-Keyauthentication) — the response includesmcpCredentialsandapiCredentialsarrays, each entry containingidandtoolinformation. Find the target credential'sidby matchingtool.idWhen creating credentials via the Contact API, the response is the full contact object — you can store the credential
idat creation time
The credential headers in the response may appear in masked form depending on the caller's identity; the revocation flow only needs the id and is not affected.
7. Two Types of Tool Credentials
Contacts can bind dedicated credentials for two types of tools, with different configuration entry points:
Purpose
AI calls MCP Server as the user
AI calls API tools as the user
Sync at login
✅ This page's setup-contact-credentials (no API Key required)
✖ Not supported (passing an API tool ID returns 400)
Contact API
POST /api/v1/contacts/{contactId}/mcp-credentials/
POST /api/v1/contacts/{contactId}/api-credentials/
Update/Delete single
PATCH/DELETE /api/v1/contacts/{contactId}/mcp-credentials/{credentialId}/
PATCH/DELETE /api/v1/contacts/{contactId}/api-credentials/{credentialId}/
Both credential endpoints of the Contact API require Api-Key authentication, and the body format is the same:
Repeated calls for the same (contact, tool) combination will update the existing record, not create duplicates. The update/delete endpoints for individual records use the credentialId to identify the target credential; see Retrieving the credentialId for how to obtain it.
8. Error Handling
400
Required field missing (e.g., sourceId); Origin not in the allowed embed domain list; toolId does not exist or is unavailable
Check the request body and WebChat settings
404
WebChat ID does not exist
Verify the webChatId is correct
429
Rate limit exceeded (30 requests per IP per minute)
Reduce call frequency and retry
500
Server error
Retry later; if the issue persists, contact the MaiAgent team
9. FAQ
Last updated
Was this helpful?
