Register a connection
Connect a new WhatsApp number before sending messages. Already connected? Start with Use a connection.
Before you begin
Obtain your workspace ID, API base URL and a backend partner credential with signup:manage. Confirm the onboarding gateway address and environment with the platform team. Keep partner credentials out of browser code.
The hosted gateway is a separate service deployed to stage/preview. Ask the platform team for its address. The browser handoff still needs stage verification and production qualification is pending.
1. Start a signup session
Your backend calls POST /partner/v1/workspaces/{workspace_id}/signup/session with an Idempotency-Key and this JSON body:
{
"purpose": "signup",
"redirect_uri": "https://your-app.example/onboarding/callback"
}
Use the HTTPS callback configured for your integration. Save the returned session_id and expires_at. A new session lasts 15 minutes. The partner builds the gateway URL in step 2.
Recover an existing attempt
Only one signup can be open for a workspace. If creation returns 409 signup_session_state_invalid with details.session_id, look up that session with the authorized backend credential and check the saved attempt. Continue a matching attempt. If an administrator chooses to abandon an earlier attempt, cancel it before creating another. A lost creation response can be retried with the same key and body.
Create and read the session on your backend
These JavaScript functions illustrate two handlers in your own app. Authenticate the user, authorize onboarding for the workspace and apply your normal CSRF protection before calling them. Supply a workspace-bound API client that sends the partner credential, parses JSON and preserves upstream error codes and retry headers. Derive the workspace and stable partner principal from the signed-in user on each request. Load the attempt from storage scoped to both values. The browser supplies neither the upstream credential nor the workspace for access control. Reuse an attempt's idempotency key across retries.
async function createSignup(workspace, attempt) {
const path = "/partner/v1/workspaces/" + encodeURIComponent(workspace.id) + "/signup";
const session = await workspace.api.post(path + "/session", {
purpose: "signup",
redirect_uri: workspace.onboardingCallback
}, {headers: {"Idempotency-Key": attempt.idempotencyKey}});
await attempt.saveSession(session.session_id, session.expires_at);
return {session_id: session.session_id};
}
async function readSignup(workspace, sessionId) {
await workspace.attempts.requireAuthorizedSession(sessionId);
const path = "/partner/v1/workspaces/" + encodeURIComponent(workspace.id) + "/signup/";
const session = await workspace.api.get(path + encodeURIComponent(sessionId));
return {state: session.state, connection_id: session.connection_id,
failed_step: session.failed_step, expires_at: session.expires_at};
}
2. Open the hosted signup popup
On Connect WhatsApp, open a blank window before the first network request. After your backend creates the session, navigate that window to <gateway>/onboard?session_id=…&workspace_id=…. The user signs in to Meta, chooses the business and phone number, and completes the requested verification steps.
This example uses your own authenticated routes. appFetch is your same-origin fetch wrapper with CSRF protection; showStatus renders the result from your backend. Configure gatewayOrigin from the platform-supplied HTTPS origin and workspaceId from the authorized workspace. Include a Check status button, initially disabled.
connectButton.onclick = async () => {
const popup = window.open("about:blank", "_blank", "popup,width=640,height=760");
if (!popup) return showError("Allow popups and try Connect WhatsApp again.");
connectButton.disabled = true;
let sessionId;
try {
const response = await appFetch("/app/whatsapp/signup", {method: "POST"});
if (!response.ok) throw new Error();
({session_id: sessionId} = await response.json());
if (!sessionId) throw new Error();
} catch {
popup.close();
connectButton.disabled = false;
return showError("Could not start signup. Retry the same attempt.");
}
const url = new URL("/onboard", gatewayOrigin);
url.searchParams.set("session_id", sessionId);
url.searchParams.set("workspace_id", workspaceId);
const statusPath = "/app/whatsapp/signup/" + encodeURIComponent(sessionId);
const terminal = ["completed", "failed", "expired", "cancelled"];
let pending = Promise.resolve();
let finished = false;
function checkStatus() {
// Serialize reads when a message and a close arrive together.
pending = pending.then(async () => {
if (finished) return;
const response = await appFetch(statusPath, {cache: "no-store"});
if (!response.ok) throw new Error();
const session = await response.json();
showStatus(session);
if (terminal.includes(session.state)) {
finished = true;
stopWatching();
checkButton.disabled = true;
connectButton.disabled = false;
}
}).catch(() => showError("Could not read signup. Select Check status to retry."));
return pending;
}
function onMessage(event) {
if (event.origin === url.origin && event.source === popup &&
event.data?.session_id === sessionId) void checkStatus();
}
function stopWatching() {
clearInterval(timer);
window.removeEventListener("message", onMessage);
}
const timer = setInterval(() => {
if (popup.closed) {
stopWatching();
void checkStatus();
}
}, 1000);
window.addEventListener("message", onMessage);
checkButton.onclick = checkStatus;
checkButton.disabled = false;
if (!popup.closed) popup.location.replace(url.href);
};
A popup message or silent close prompts a session read. Even a message named WA_ONBOARDING_SUCCESS requires that read. Event names and fields still need confirmation on stage. If the user reloads your app, recover their saved attempt on the backend and offer Check status again.
For a full-page redirect, add the platform-approved HTTPS return_url to the gateway URL so the user can return to your app. The redirect_uri in the create-session body is the integration’s onboarding callback. On return, look up the saved attempt before displaying a result.
With the hosted gateway, your application does not call signup/complete. A self-hosted Meta login is a separate integration requiring your own registered app/domain configuration. In that flow, your backend sends the temporary Meta authorization code to Complete signup. The platform exchanges the code, performs phone registration and webhook subscription, and binds the connection. An omitted two-step verification PIN is generated securely; an existing mismatched PIN requires recovery.
3. Read the authoritative result
Read GET /partner/v1/workspaces/{workspace_id}/signup/{session_id} from your backend. Popup messages and redirects are only hints to perform this read. Validate the gateway origin, popup window and session identity before accepting a browser hint.
| Session state | Next action |
|---|---|
created | Signup is still pending. |
exchanging | Code exchange is in progress. Wait for Retry-After before reading again. |
completed | Save the returned connection ID and inspect the connection. |
awaiting_pin | Let the gateway collect the six-digit PIN, or collect it securely in your app and call Resume signup from the backend within 30 minutes. Keep the PIN out of logs. |
partial | Resume the unfinished registration steps; do not reuse the authorization code. |
expired | Create a new session. |
cancelled / failed | Inspect the error before starting again. |
Use Signup status and Resume signup for the full state and recovery contract. A successful HTTP response alone does not mean registration is complete: pin_required and registration_incomplete are action-required results. After completion, check the connection and verify that its sender matches the number the user selected. Continue to billing and template readiness before the first send.
Adding a new number creates a separate connection. It does not replace an existing number. For an existing number that needs authorization renewed, use Reconnect, not another signup.
Test the flow on stage
Use the sandbox workspace, its pt_test_ partner key and a Meta test number. Confirm the deployed gateway address separately. Exercise success, silent closure, PIN recovery and an expired session. Compare every UI outcome to the backend session read and verify the saved connection ID belongs to the intended sender. A sandbox key is scoped to sandbox resources. Production qualification is pending.
Continue with Use a connection, then Billing readiness.