Building an app
A step-by-step guide to building a VPN app on the App API: startup config, sign-in, per-device VPN credentials, assembling OpenVPN and WireGuard configurations, and a robust connection fallback strategy.
All requests in this guide go to the App API and carry
X-App-Key (your public app key) and Accept: application/json. Signed-in
requests also carry Authorization: Bearer <token>. Examples use the base URL
https://mercury.vpnbuilder.org.
1. App startup: GET /v2/app/config
Call it on every launch (no token needed). platform is required:
ios, android, macos, windows or linux.
curl 'https://mercury.vpnbuilder.org/v2/app/config?platform=android&version=3.2.0' \
-H 'Accept: application/json' -H 'X-App-Key: pk_acme_3Jx9cTq0aVb1mN8rLw2yKp5sDf7gHj4z'
{
"latest_version": "3.4.0",
"min_supported_version": "3.0.0",
"update_url": "https://play.google.com/store/apps/details?id=com.acme.vpn",
"links": {
"website": "https://acme.example.com",
"pricing": "https://acme.example.com/pricing",
"account": "https://acme.example.com/account",
"help": "https://acme.example.com/help",
"privacy": "https://acme.example.com/privacy",
"terms": "https://acme.example.com/terms",
"support_email": "support@acme.example.com"
},
"announcement": {
"id": "winter-sale-2026",
"title": "Winter sale",
"body": "Get 50% off a yearly plan this week.",
"url": "https://acme.example.com/pricing"
},
"connection_limit": 5
}
- Every value may be
nullwhen the brand has not set it; hide the corresponding UI. - Updates: if the running version is lower than
min_supported_version, block the app with an "update required" screen linking toupdate_url; lower thanlatest_version, show a dismissable prompt. Compare versions numerically (3.10.0>3.9.0). - Announcement: show it once and store its
idwhen dismissed; a new announcement has a newid(when the operator sets none it is a hash of the title and body, so editing the text shows it again).
2. Accounts
| Action | Call |
|---|---|
| Register | POST /v2/auth/register {name, email, password, device_name?} → 201 {token, user} |
| Sign in | POST /v2/auth/login {email, password, device_name?} → {token, user} |
| Forgot password | POST /v2/auth/forgot-password {email} → 202 {} (always, even for unknown emails) |
| Reset password | POST /v2/auth/reset-password {email, code, password, device_name?} → {token, user} |
| Sign out | POST /v2/auth/logout → 204 |
| Profile | GET /v2/me, PATCH /v2/me {name}, PUT /v2/me/password {current_password, password} |
- Passwords: 8–253 characters, no line breaks.
409 account_existson register means the email already has an account at your brand (including customers from your older systems): send the user to sign in. Their first sign-in migrates them automatically.- The reset code is 6 digits, emailed with your brand's sender, valid 30
minutes, 5 attempts. A wrong or expired code is
422 invalid_reset_code. 401 invalid_credentials= wrong email or password.- The
user.subscriptionobject tells you whether to show the paywall (active), the plan (type,expires_at), where it was bought (source) and amanage_urlfor a "Manage subscription" button.
3. Device credentials
Each install gets its own random VPN username and password, created by VPNBuilder. The customer never types or sees them, and their account password is never used for the VPN.
- On first launch generate a random UUID and keep it for the lifetime of the
install. Send it as
X-Device-Id, withX-Device-Name(e.g. "Ada's iPhone") andX-Device-Platform. - Before connecting, call
GET /v2/vpn/profile?server_id=<selected server id>. It needs an active plan (403 subscription_requiredotherwise: show the paywall).
curl 'https://mercury.vpnbuilder.org/v2/vpn/profile?server_id=100003' \
-H 'Accept: application/json' \
-H 'X-App-Key: pk_acme_3Jx9cTq0aVb1mN8rLw2yKp5sDf7gHj4z' \
-H 'Authorization: Bearer 12|kX8bS0mQ2v7tYf4nR1cW9eJ6hL3pA5zD' \
-H 'X-Device-Id: 6f1c2d4e-8a7b-4c3d-9e2f-1a0b9c8d7e6f' \
-H "X-Device-Name: Ada's iPhone" -H 'X-Device-Platform: ios'
{
"profile": {
"template": "client\ndev tun\nnobind\nremote-cert-tls server\n...\n<ca>\n-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----\n</ca>\n",
"transports": [
{"id": "udp", "proto": "udp", "port": 1194},
{"id": "tcp", "proto": "tcp", "port": 443},
{"id": "wireguard", "proto": "wireguard", "port": 51820}
],
"credentials": {"username": "acme_q3v8k2m9x1zr", "password": "Yt7wq2Lm9pKs4ZxV"},
"fleet": "nodes",
"wireguard": {
"server_public_key": "hT5u0Q2y8m3nB7cX1vL9kR4sW6eD0fJ2aP8gZ5tY3iM=",
"endpoint": "203.0.113.20:51820",
"allowed_ips": "0.0.0.0/0, ::/0",
"dns": ["10.100.0.1"],
"address": "10.100.0.7/32",
"address_v6": "fd5b:8b:0:100::7/128",
"persistent_keepalive": 25
}
}
}
- The same device always gets the same credentials. If the user revoked the device (from another device or your website), the next profile call issues a new username and password: always use the latest values.
fleetis present only while the platform runs its newer node fleet (legacyornodes). Template and transports differ per fleet, the credentials do not. Cache the profile per fleet, and always sendserver_idso you get the template matching the server you connect to.wireguardis present only for a node that serves WireGuard (see below).- Re-fetch the profile on every connect; it is cheap and picks up revocations, plan changes and template updates.
4. Servers
GET /v2/servers (any signed-in user, no plan needed):
{
"servers": [
{
"id": 100003,
"country": {"code": "GB", "name": "United Kingdom"},
"city": "London",
"hostname": "lon-1.vpn.example.net",
"ip": "203.0.113.20",
"latitude": 51.5072,
"longitude": -0.1276,
"load": 23,
"fleet": "nodes",
"protocols": ["openvpn-udp", "openvpn-tcp", "wireguard"]
}
]
}
loadis 0–100, ornullwhen unknown. Use it to pick the "fastest" server.hostnamemay benull;ipmay hold a hostname on older servers. Accept either.fleetappears on every server once the node fleet is enabled; node servers also list theirprotocols.wireguardis listed only once the node is ready for it.- Treat
idas opaque (node ids happen to be large numbers such as100003).
5. OpenVPN: assembling the config
The template is a complete OpenVPN client config without the remote,
proto and auth-user-pass lines. Your app appends exactly these three lines
for the chosen server and transport, and supplies the credentials itself:
<template>
remote <server.hostname, else server.ip> <transport.port>
proto <transport.proto>
auth-user-pass
Worked example for London over UDP:
client
dev tun
nobind
remote-cert-tls server
data-ciphers AES-256-GCM:CHACHA20-POLY1305:AES-128-GCM
redirect-gateway def1 ipv6
explicit-exit-notify 2
verb 3
<ca>
-----BEGIN CERTIFICATE-----
MIIBszCCAVmgAwIBAgIU...(truncated)...
-----END CERTIFICATE-----
</ca>
<tls-crypt>
-----BEGIN OpenVPN Static key V1-----
6acef03f62675b4b1bbd03e53b187727...(truncated)...
-----END OpenVPN Static key V1-----
</tls-crypt>
remote lon-1.vpn.example.net 1194
proto udp
auth-user-pass
Then hand profile.credentials.username / password to your OpenVPN library
(e.g. OpenVPN3 ProvideCreds, or a temporary credentials file on desktop).
Never write the password into the config text you might log or share.
Transports come from profile.transports, in preference order (by default
udp 1194 and tcp 443). Only use the OpenVPN ones (proto udp/tcp) for
OpenVPN.
6. WireGuard
WireGuard is available on node servers whose protocols include wireguard.
- Generate a key pair on the device (x25519, e.g. with the WireGuard library). The private key never leaves the device.
- Register the public key for this device (active plan required):
curl -X PUT https://mercury.vpnbuilder.org/v2/wireguard/key \
-H 'Accept: application/json' -H 'Content-Type: application/json' \
-H 'X-App-Key: pk_acme_3Jx9cTq0aVb1mN8rLw2yKp5sDf7gHj4z' \
-H 'Authorization: Bearer 12|kX8bS0mQ2v7tYf4nR1cW9eJ6hL3pA5zD' \
-H 'X-Device-Id: 6f1c2d4e-8a7b-4c3d-9e2f-1a0b9c8d7e6f' \
-d '{"public_key": "pG3rV6m2wq9Ls1Zt8Yc4Hn0Bk7Jd5Fa2Xe3Uo6Ri9Mw="}'
{"wireguard": {"address": "10.100.0.7/32", "address_v6": "fd5b:8b:0:100::7/128", "dns": ["10.100.0.1"]}}
- The key must be a base64 32-byte key (44 characters), else
422 validation_failed(fields.public_key). - A key already used by another device is
409 wireguard_key_in_use: generate a new pair. - PUT again with a new key to rotate it; the addresses stay the same. The device has the same tunnel address on every server.
- Revoking the device clears its key: after the user signs in again, generate and PUT a new key.
- Fetch the profile with the node's id:
GET /v2/vpn/profile?server_id=100003. Itswireguardblock has the server key and endpoint.addressisnulluntil the device has registered a key. - Build the config:
[Interface]
PrivateKey = <device private key, generated on the device>
Address = 10.100.0.7/32, fd5b:8b:0:100::7/128
DNS = 10.100.0.1
[Peer]
PublicKey = hT5u0Q2y8m3nB7cX1vL9kR4sW6eD0fJ2aP8gZ5tY3iM=
Endpoint = 203.0.113.20:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
Address = address plus address_v6 when it is not null; DNS = the dns
array joined with commas. A freshly registered key is active on the servers
within about 5 seconds.
7. Fallback strategy
Recommended connect logic:
- Fetch
GET /v2/vpn/profile?server_id=<id>(handles revocations and plan changes). On403 subscription_requiredshow the paywall; on401sign the user out. - If the server's
protocolsincludewireguardand the profile has awireguardblock with a non-nulladdress, connect with WireGuard. (Ifaddressis null, PUT your key first and re-fetch.) - Otherwise, or if WireGuard gets no handshake within ~5–10 seconds, use OpenVPN over the first UDP transport, then fall back to TCP 443 (works on most restrictive networks).
- If OpenVPN fails with
AUTH_FAILED:GET /v2/me: ifsubscription.activeis false, show the paywall;GET /v2/sessions: ifenforcedis true andsessionshaslimitentries, tell the user they are connected on too many devices and show where (a WireGuard device over the limit simply gets no handshake);- otherwise re-fetch the profile once (credentials may have been reissued) and retry.
8. Devices and sessions screens
GET /v2/deviceslists the account's app installs;currentmarks the one whoseX-Device-Idyou sent.DELETE /v2/devices/{id}revokes a lost device: its VPN credentials stop working and its app is signed out.GET /v2/sessionsreturns{limit, enforced, sessions}: the open VPN connections of the whole account (apps, routers and old-app logins), with server and device, oldest first.
9. Routers and other OpenVPN clients
Users who want to set up a router need a manual credential (a device credential is tied to an app install):
POST /v2/vpn/manual-credentials {"label": "Living room router"}(active plan) →201 {credential: {id, label, username, password, created_at, last_used_at}}. The password is shown only in this response: display it once.GET /v2/vpn/ovpn?server_id=<id>&transport=udpreturns a ready.ovpnfile (text/plain,Content-Disposition: attachment) withremote,protoandauth-user-passalready added; the router asks for the username/password. No plan needed to download.GET /v2/vpn/manual-credentialslists them (withlast_used_at),DELETE /v2/vpn/manual-credentials/{id}revokes one.
10. Showing the exit IP
GET /v2/ip (app key only) returns {"ip": "203.0.113.20"}, the address
the request came from. Call it through the tunnel after connecting to show
the user their VPN IP without a third-party service. Nothing is stored.
11. Account deletion
App stores require in-app account deletion. Use
DELETE /v2/me {"password": "…"} → 204: it deletes the account, all its VPN
credentials, devices and tokens. A wrong password is 401 invalid_credentials.
Remind the user to cancel their store subscription (show manage_url):
deleting the account does not cancel billing at Apple or Google.
12. Support
Add a "Send logs" button that calls POST /v2/support/logs and shows the
returned reference. See Support reports.