iOS integration guide
Everything an iOS app needs to talk to the Datacove API: signing users in with a one-time code, keeping their session alive, running workflows, receiving results, and selling credits or a subscription through the App Store with StoreKit 2.
01Before you start
You need two things from your Datacove tenant administrator before writing any code.
An IOS API key
A tenant API key whose type is IOS. It identifies your app and is sent as the x-api-key header on the sign-in calls and on file uploads. The OTP routes reject keys of any other type.
The API base URL
Every path in this guide is relative to BASE_URL, which already includes the /api prefix and any path prefix your deployment adds in front of it.
Anything shipped inside an app bundle can be extracted. The key only unlocks the sign-in endpoints and tenant-scoped public data; everything user-specific requires the user's own token. Keep it out of logs, and supply it through a build configuration file rather than hard-coding it in source.
| Credential | Sent as | Used for |
|---|---|---|
| API key | x-api-key: <key> | Requesting and verifying OTPs, uploading files with /media/v1/upload |
| Access token | Authorization: Bearer <token> | Everything the signed-in user does: profile, workflows, reports, purchases. Valid for 30 minutes. |
| Refresh token | JSON body | Only POST /auth/v1/refresh, to get a new token pair. Valid for 7 days and rotated on every use. |
| App build | X-App-Build: <CFBundleVersion> | Every request, from the app and the Safari extension. Lets the server tell outdated builds to update (see App updates). |
| App version | X-App-Version: <version> | Send it on POST /workflows/v1/execute, from the app and from the Safari extension: CFBundleShortVersionString, for example 1.2.0. |
02How it fits together
The user never sets a password. They prove they own a phone number or email address with a six-digit code, and the app receives a token pair it keeps refreshing for as long as the user stays signed in.
03Sign in
Offer phone, email, Google, or any mix. Phone and email work the same way: request a code, verify it, and activate the account the first time it signs in. Google skips the code. Each phone number or email address maps to one account per tenant, created automatically the first time it is used.
3 requests per minute and 10 per hour from one IP address. A 429 means wait; show a countdown rather than retrying in a loop.
Phone number
Creates the account if this number is new, then sends a six-digit code by SMS, or by WhatsApp when the tenant has WhatsApp delivery enabled. The response is identical for new and existing numbers, so it never reveals whether an account exists.
| Field | Type | Notes |
|---|---|---|
| phoneNumberrequired | string | International format, digits only, for example 14165550199. A leading + is accepted and stripped. |
| nameoptional | string | Display name. Used only when the account is new; ignored afterwards. |
| channeloptional | "sms" | "whatsapp" | Defaults to sms. Send the same value again on verify. |
| languageoptional | string | Preferred language for emails and messages, as a two-letter code such as en or hi. Used only when the account is new; defaults to en. |
| Status | Meaning |
|---|---|
| 200 | Code sent. |
| 400 | Invalid number, or WhatsApp was requested but isn't enabled for this tenant. |
| 429 | Rate limited, or (WhatsApp) a code was sent to this number less than 60 seconds ago. |
| 500 | WhatsApp refused to send the code. Nothing was sent, so the user can retry straight away. |
Codes expire 10 minutes after they're sent and lock after 5 wrong attempts. A locked or expired code can't be retried; request a new one.
| Field | Type | Notes |
|---|---|---|
| phoneNumberrequired | string | The same number used on request. |
| otprequired | string | Exactly six digits. |
| channeloptional | "sms" | "whatsapp" | Must match the request. |
{
"accessToken": "eyJhbGciOiJIUzI1NiIs...",
"refreshToken": "eyJhbGciOiJIUzI1NiIs...",
"expiresIn": 1800
}| Status | Meaning |
|---|---|
| 401 | Wrong code. |
| 403 | Locked after 5 wrong attempts. |
| 404 | No account for this number. Request a code first. |
| 410 | Code expired. |
struct TokenPair: Codable { let accessToken: String; let refreshToken: String; let expiresIn: Int }
struct DatacoveClient {
let baseURL: URL // e.g. https://api.example.com/api
let apiKey: String // the IOS tenant API key
func requestMobileOtp(phone: String, name: String? = nil) async throws {
_ = try await post("auth/v1/mobile/request-otp",
body: ["phoneNumber": phone, "name": name, "channel": "sms"], withKey: true)
}
func verifyMobileOtp(phone: String, otp: String) async throws -> TokenPair {
let data = try await post("auth/v1/mobile/verify-otp",
body: ["phoneNumber": phone, "otp": otp, "channel": "sms"], withKey: true)
return try JSONDecoder().decode(TokenPair.self, from: data)
}
func post(_ path: String, body: [String: String?], withKey: Bool = false, token: String? = nil) async throws -> Data {
var request = URLRequest(url: baseURL.appendingPathComponent(path))
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
if withKey { request.setValue(apiKey, forHTTPHeaderField: "x-api-key") }
if let token { request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") }
request.httpBody = try JSONSerialization.data(withJSONObject: body.compactMapValues { $0 })
let (data, response) = try await URLSession.shared.data(for: request)
guard let http = response as? HTTPURLResponse, (200..<300).contains(http.statusCode) else {
throw APIError.from(data: data, response: response)
}
return data
}
}Identical to the phone flow, with an email address instead of a number and no channel choice. Requesting a new code within 60 seconds of the last one returns 429.
{ "email": "jane@example.com", "name": "Jane Doe", "language": "hi" }{ "email": "jane@example.com", "otp": "123456" }Sign the user in with Google on the device, then send the ID token Google returns. With the Google Sign-In SDK, set serverClientID to the tenant's web OAuth client ID. The backend checks every platform's token against that one client ID, so a token issued for the app's own client ID is rejected. There is no code to request and no activate step: a Google account is active from the start.
| Field | Type | Notes |
|---|---|---|
| idTokenrequired | string | The Google ID token, sent in the body. |
| languageoptional | string | Preferred language for emails and messages, as a two-letter code such as en or hi. Used only when the account is new; defaults to en. |
| countryquery | string | Two-letter country code. Required the first time an email signs in, when the account is created; ignored afterwards. |
| Status | Meaning |
|---|---|
| 200 | { accessToken, refreshToken, expiresIn }, or a 2FA challenge (see below). |
| 400 | New account and no country was sent. |
| 401 | The ID token is invalid or expired, was issued for another client ID, or Google sign-in isn't configured for this tenant. |
| 403 | The account is disabled. |
| 429 | Rate limited. |
Google and email codes sign in to the same account for the same address, here, in the other mobile app and on the web, with the same wallet, history and plans. An account made with an email code becomes a full account the first time the user signs in with Google. Phone-number accounts are separate, because they are identified by the number.
If the user has turned on two-factor authentication on the web, google-login and email/verify-otp return { "twoFactorRequired": true, "challengeToken": "..." } instead of tokens. Ask for the six-digit code from their authenticator app (or a backup code) and send { challengeToken, code } to POST /auth/v1/2fa/verify with the API key; it returns the usual token pair. The challenge expires after 5 minutes. Accounts without two-factor authentication never see this response.
Activate and load the profile
A brand-new account comes back from verify with status: "CREATED" in its access token. Call activate once to move it to ACTIVE; it returns a fresh token pair that replaces the one you just stored. The call is idempotent, so if you're unsure whether it succeeded (a timeout, a dropped connection), just call it again.
Returns the same { accessToken, refreshToken, expiresIn } shape. 400 means the account is in a state that can't be activated from the app.
{
"name": "Jane Doe",
"phone": "14165550199",
"country": "CA",
"timezone": "America/Toronto",
"status": "ACTIVE",
"language": "hi",
"wallet": { "WALLET": 25, "TRIAL": 0, "SUBSCRIPTION": 0 }
}wallet.WALLET is the user's spendable credit balance. A new account can start with a signup bonus the tenant configures per platform (the amount for ios applies to accounts created in this app, whichever way they sign in), so don't assume it starts at zero.
To change the name, phone number or preferred language, use PATCH /users/v1/profile. Offer the language your users' messages should arrive in on the sign-up screen (send it as language on the first request-otp or google-login call) and in settings. App Store rules require in-app account deletion: see Deleting the account.
Deleting the account
Before an account is deleted, the person confirms who they are again, even though they are signed in, so an unlocked phone or a copied token can't erase it. Deletion is immediate and cannot be undone: the person's details, sessions, reports and files are removed, and a later sign-in with the same email or number starts a new account. Unused credits are lost, and an App Store subscription is not cancelled: link to the App Store's subscription settings. Say both on the confirmation screen.
Returns { "methods": [ { "method": "EMAIL_CODE" }, { "method": "PHONE_CODE", "channels": ["sms", "whatsapp"] } ] }, the proofs this account can give. Offer only these.
| Method | What the person does | Sent with the delete |
|---|---|---|
| PASSWORD | Types their password. | password |
| EMAIL_CODE | Enters the six-digit code emailed to them. | code |
| PHONE_CODE | Enters the six-digit code sent by SMS or WhatsApp. | code |
| Signs in with Google again, with the same address. Send the new ID token within 5 minutes. | idToken | |
| TWO_FACTOR | Enters a code from their authenticator app, or a backup code. | code |
Send { "method": "PHONE_CODE", "channel": "sms" } (channel only for PHONE_CODE). For a code method, the code goes to the account's own email or phone, at most once a minute. Returns { challengeId, method, channel, sentTo, expiresAt }; sentTo is masked, for example r***@example.com. A challenge lasts 10 minutes and allows 5 wrong answers.
Send { challengeId } with password, code or idToken, as in the table above. 204 means the account is gone: clear the stored tokens and return to sign-in. A retry with the same token after the account is gone also answers 204.
| Status | Meaning |
|---|---|
| 204 | Deleted. |
| 400 | METHOD_NOT_AVAILABLE: offer one of the returned methods instead. |
| 401 | PROOF_INCORRECT: let the person try again. CHALLENGE_INVALID: start a new challenge. |
| 403 | CHALLENGE_LOCKED after 5 wrong answers: start a new challenge. |
| 409 | LEGAL_HOLD: the account can't be deleted right now. Show the message and point to support. |
| 410 | CHALLENGE_EXPIRED after 10 minutes: start a new challenge. |
| 429 | A code was sent less than a minute ago, or too many attempts. Five a minute, twenty an hour. |
| 502 | CODE_NOT_SENT: nothing was sent; try again or offer another method. |
Disabled accounts. A disabled account can't sign in, so its owner deletes it signed out, with the API key: POST /users/v2/disabled-account/deletion-challenge with { email } or { phoneNumber, channel }, then DELETE /users/v2/disabled-account with { challengeId, code }. The first call answers the same way whether or not a disabled account matches, so show "If an account matches, we've sent a code" either way.
Accounts nobody uses. An account unused for 3 years is deleted the same way, after one warning 30 days before (by email, and by WhatsApp or SMS where the tenant has set it up). Any sign-in, token refresh or run keeps it for another 3 years, and an account with an active plan is never deleted.
The old route. DELETE /users/v1/account still deletes without asking the person to confirm, and answers with Deprecation and Sunset: Sat, 01 May 2027 00:00:00 GMT headers. Move to the v2 routes; v1 is removed once every supported build calls v2.
04Keeping the session alive
Access tokens last 30 minutes. Refresh tokens last 7 days and change every time you use one, so a user who opens the app at least once a week stays signed in indefinitely.
{ "refreshToken": "eyJhbGciOiJIUzI1NiIs..." }Returns a new { accessToken, refreshToken, expiresIn }. Always save the new refresh token: the one you sent stops working once the new one is issued.
Rules for a well-behaved client
- Store tokens in the Keychain. If an app extension shares the session, put the item in a shared Keychain access group so both read the same, latest tokens.
- Refresh proactively. Refresh about a minute before
expiresInelapses, and also when any request returns401, then retry that request once. - Refresh one at a time. Route every refresh through a single actor so concurrent requests share one refresh. Two refreshes that arrive within 30 seconds of each other with the same token both succeed and return the same new token. Anything later with an old token is treated as a stolen token and ends the session.
- Re-read before refreshing. When an app and an extension share tokens, read the Keychain again just before refreshing. Another process may already have rotated the token.
actor SessionManager {
private let client: DatacoveClient
private let keychain: TokenKeychain // shared access group if an extension uses it
private var inFlight: Task<TokenPair, Error>?
init(client: DatacoveClient, keychain: TokenKeychain) {
self.client = client
self.keychain = keychain
}
/// Returns a usable access token, refreshing it at most once at a time.
func validAccessToken(rejected: String? = nil) async throws -> String {
if let task = inFlight { return try await task.value.accessToken }
guard let current = keychain.read() else { throw APIError.signedOut(code: nil) }
// Another caller (or process) already rotated it: use the newer token.
if let rejected, current.accessToken != rejected { return current.accessToken }
let task = Task { () throws -> TokenPair in
let data = try await client.post("auth/v1/refresh", body: ["refreshToken": current.refreshToken])
let pair = try JSONDecoder().decode(TokenPair.self, from: data)
try keychain.write(pair) // persist before anyone uses it
return pair
}
inFlight = task
defer { inFlight = nil }
do {
return try await task.value.accessToken
} catch let APIError.http(status, code, _) where status == 401 {
keychain.clear()
throw APIError.signedOut(code: code) // SESSION_EVICTED, TOKEN_REUSED, ...
}
}
}When a refresh is rejected
A 401 from refresh, or from any authenticated request, carries a machine-readable code inside error. Use it to tell the user why they were signed out.
{
"statusCode": 401,
"timestamp": "2026-10-02T09:30:00.000Z",
"path": "/api/auth/v1/refresh",
"error": {
"statusCode": 401,
"error": "Unauthorized",
"message": "You were signed out because this account signed in on another device.",
"code": "SESSION_EVICTED"
}
}| code | What happened | Suggested message |
|---|---|---|
| SESSION_EVICTED | The account signed in on more than 5 devices, and this was the oldest session. | "You were signed out because your account was used on another device." |
| SESSION_REVOKED | This session was signed out: logout, "sign out of all devices", signed out from another device's session list, or the account was disabled. | "You've been signed out." |
| TOKEN_REUSED | A refresh token that had already been replaced was used again, so the session was ended as a precaution. | "For your security, please sign in again." |
| REFRESH_INVALID | The refresh token is expired (older than 7 days), malformed, or unknown. | "Your session has expired. Please sign in again." |
Devices and signing out
An account can be signed in on up to 5 devices at once. Signing in on a sixth ends the least recently created session. The same endpoints let you build a "signed-in devices" screen:
| Call | Does |
|---|---|
| GET /auth/v1/sessions | Lists the user's sessions with createdAt, lastUsedAt, the app type that created it, and current: true on this one. |
| DELETE /auth/v1/sessions/:sessionId | Signs out one device. Its access token stops working immediately. |
| GET /auth/v1/logout | Signs out this device. |
| DELETE /auth/v1/sessions | Signs out every device, including this one. |
05Running workflows
A workflow is an AI task with a defined set of inputs. Fetch its definition, render a form from its inputs, then execute it. Each run costs the credits configured for that workflow, deducted from wallet.WALLET.
Discover workflows
GET /workflows/v1/:pageNo/:pageSize returns { data, meta }, filtered to what your tenant offers. Optional query parameters: search, category, featured. Then fetch one by its id, for example GET /workflows/v1/wf-sec-001, to get its inputs.
Build the form from inputs
[
{
"key": "url",
"label": "Website address",
"type": "text",
"placeholder": "https://example.com",
"validation": { "required": true }
},
{
"key": "document",
"label": "Document",
"type": "file",
"fileOptions": { "accept": ".pdf,.docx", "maxSizeMb": 10, "multiple": false }
}
]| Field | Use |
|---|---|
| key | The property name to send inside input. |
| type | text, text-area, number, date, time, location, dropdown, select, file, object, array. |
| options | Choices for dropdown and select. |
| fileOptions | Allowed formats (accept), size limit in MB (maxSizeMb), and whether several files are allowed. The server enforces all three, so read them from here rather than hardcoding them. |
| validation | Rules such as required. The server enforces them too. |
| Field | Type | Notes |
|---|---|---|
| idrequired | string | The workflow id, for example wf-sec-001. |
| inputrequired | object | One property per input key. |
| countryoptional | string | ISO country code, for workflows whose output varies by country. |
Headers: Authorization: Bearer <token> and X-App-Version: <version>, from the app and from the Safari extension. Plan access is described in Trial and entitlement.
Text-only inputs: send JSON. File inputs: send multipart/form-data with id (and country, if used) as plain text fields, every non-file workflow input in one text field named input as a JSON string (send {} if there are none), and each file as a part named after its input key. A missing, empty, unreadable or oversized file, or one whose content isn't an accepted format, is rejected with 400 before anything runs (see file errors).
func executeWithFile(workflowId: String, fileKey: String, fileURL: URL, mimeType: String, token: String) async throws -> Data {
let boundary = "Boundary-\(UUID().uuidString)"
var request = URLRequest(url: baseURL.appendingPathComponent("workflows/v1/execute"))
request.httpMethod = "POST"
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue(appVersion, forHTTPHeaderField: "X-App-Version") // CFBundleShortVersionString
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var body = Data()
func field(_ name: String, _ value: String) {
body.append("--\(boundary)\r\nContent-Disposition: form-data; name=\"\(name)\"\r\n\r\n\(value)\r\n")
}
field("id", workflowId) // plain text field
field("input", "{}") // JSON string of the non-file inputs, {} if none
body.append("--\(boundary)\r\n")
// Each file part is named after its input key.
body.append("Content-Disposition: form-data; name=\"\(fileKey)\"; filename=\"\(fileURL.lastPathComponent)\"\r\n")
body.append("Content-Type: \(mimeType)\r\n\r\n")
body.append(try Data(contentsOf: fileURL))
body.append("\r\n--\(boundary)--\r\n")
let (data, _) = try await URLSession.shared.upload(for: request, from: body)
return data
}
private extension Data {
mutating func append(_ string: String) { append(Data(string.utf8)) }
}Running workflows with v2
Same callers, credits, responses and errors as POST /workflows/v1/execute; only the request differs. New builds should use v2: v1 is deprecated and will be removed once every client has moved.
| Form field | Notes |
|---|---|
| workflowIdrequired | The workflow id, for example wf-sec-001 (v1 called it id). |
| input.<key> | One field per workflow input, named after its key. Numbers as decimal text, object and array inputs as JSON text, everything else as text. |
| input.<key> (file) | A file input is the file itself, as a file part with the same name. Repeat the part for inputs that accept several files. Text in a file input (a link, an S3 address, an id) is refused with FILE_INPUT_NOT_A_FILE. |
| country, name, email, couponCodeoptional | As in v1. |
| callAsyncoptional | true for sk_ and mk_ keys only. In v2 it is a form field, not a query parameter. |
Files are stored privately, exactly as sent: send the original file, without compressing or converting it. A run that doesn't start keeps no files. Other 400 codes: UNKNOWN_FIELD (a field that is neither a run field nor an input of this workflow) and INVALID_JSON. A JSON body gets 415. 403 UPLOADS_NOT_ENABLED means the tenant can't accept files yet. 429 FILE_LIMIT_REACHED means the user or the app has sent too many files for now: wait the number of seconds in the Retry-After header (also error.retryAfterSeconds) and tell the user, rather than retrying. Don't set Content-Type yourself; let the HTTP client add the multipart boundary.
var form = MultipartForm() // any multipart helper
form.addField("workflowId", "wf-sec-008")
form.addField("input.question", "Is this clause fair?")
form.addFile("input.document", fileName: "contract.pdf",
mimeType: "application/pdf", data: originalData) // not re-encoded
var request = URLRequest(url: baseURL.appendingPathComponent("workflows/v2/execute"))
request.httpMethod = "POST"
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
request.setValue(form.contentType, forHTTPHeaderField: "Content-Type")
request.httpBody = form.bodyTwo kinds of response
Instant result
Chat and analysis workflows answer in the same request. The 201 body is the result itself.
Report
Longer workflows start a run and return straight away with { "success": true, "eventId": "…", "message": "…" }. Keep the eventId and collect the result as shown in the next section. Reports can take several minutes.
Each workflow always responds the same way, so a client can simply check for eventId in the response.
06Getting report results
Use the realtime channel to be told the moment a report is ready, and keep polling as a fallback. Both use the eventId from execute.
Realtime (recommended) →
Mint a ticket, connect with Socket.IO, join the run's room, and receive workflow_status_update when it finishes. The Realtime guide has a complete Swift example.
Polling
Call GET /workflows/reports/v1/:eventId every 5 seconds. A run can take up to 20 minutes, so keep polling at least that long before giving up.
| GET /workflows/reports/v1/:eventId | Meaning |
|---|---|
| 200 { "status": "GENERATING" } | Still running. |
| 200 (report body) | Finished. The body is the report. |
| 404 | The run failed, or the report doesn't exist. These look the same on purpose. Credits are refunded on failure. |
| 410 REPORT_DELETED | The report was deleted by its owner or with their account. Its content is gone for good, for everyone it was shared with too. Show "This report has been deleted" and stop polling. |
A user's past runs are listed by GET /workflows/reports/v1/history/:page/:limit. The list includes runs the same account made on the web or in the other app; add ?source=ios to show only this app's runs. Each item carries its source. The credit history, GET /credits/v1/history/:page/:limit, takes the same filter. If the app goes to the background during a long run, poll the report when it returns to the foreground.
Opening files sent with a run
Files sent with a v2 run are private. List a run's files with the route below, then ask the backend for a link each time the user opens one; the link is signed, works for 10 minutes, and points at the product's own file domain (for example files.legalaware.ai). Don't store links or put them in logs: anyone holding one can open the file until it expires.
Returns { "data": [...] }, one item per file sent with the run: uploadId, inputKey, fileName, mimeType, sizeBytes, blocked (true when the file was found to contain malware) and linkRoute, the link route below for that file. The check, the password query parameter and the errors are the report's own. Files from runs started before the v2 run route are not listed.
Returns { "url", "expiresAt" }. The same account that may read the run's report may open its files, with the report's password query parameter if it has one. Images, video, audio and PDF open in place; other files download.
| Status | Meaning |
|---|---|
| 200 | Open url right away, in a browser view or the system viewer. |
| 401 | The report is password-protected and the password is missing or wrong. |
| 403 FILE_BLOCKED | The file was found to contain malware. Nobody can open it; tell the user it was blocked. |
| 404 | No such file for this account: unknown, deleted with its report, or someone else's. |
| 409 FILE_LINKS_NOT_ENABLED | The product has no file domain yet. |
The files the user sent, newest first, as { "data": [...], "meta": { "total", "page", "limit", "totalPages" } }. Each item has uploadId, reportId, workflowId, inputKey, fileName, mimeType, sizeBytes and createdAt. A file goes when its report or the account is deleted; there is no separate delete.
struct FileLink: Decodable { let url: URL; let expiresAt: Date }
// Ask for a fresh link each time the user opens the file; never cache it.
func openFile(uploadId: String, token: String) async throws {
var request = URLRequest(url: baseURL.appendingPathComponent("media/v2/files/\(uploadId)/link"))
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let (data, _) = try await URLSession.shared.data(for: request)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let link = try decoder.decode(FileLink.self, from: data)
await UIApplication.shared.open(link.url)
}07App Store purchases
Apps can sell two kinds of product with StoreKit 2. Your tenant administrator creates both in the Datacove catalog, using the same product ids you set up in App Store Connect.
Credit packages
Consumable in-app purchases such as "500 credits". A verified purchase adds the package's credits to wallet.WALLET. Other product types are rejected for credit packages.
Plan (service entitlement)
A plan that unlocks specific workflows: an auto-renewable subscription (monthly or annual) or a one-year plan sold as a non-renewing subscription. The tenant decides which plans are on sale. A subscription's free trial is an introductory offer on the product in App Store Connect; a server trial, if any, is for one-year plans only.
Trial and entitlement
Workflows covered by a plan are available while the user has an active trial or an active plan. Without either, executing one returns 403 with code: "ENTITLEMENT_INACTIVE". A run that is allowed is charged the workflow's own credit price like any other run; a workflow included in a plan is normally free. The account is what holds the plan, so it follows the user to every device they sign in on. Read the state at launch, when the app returns to the foreground, and after every purchase:
{
"trial": { "status": "not_started" },
"trialOffer": { "available": false },
"entitlement": {
"status": "active",
"productId": "650f1c2e9b1d4a0012ab34cd",
"platform": "apple",
"currentPeriodEnd": "2026-11-01T08:00:00.000Z"
},
"plans": [
{
"productId": "650f1c2e9b1d4a0012ab34cd",
"platformProductId": "com.example.app.yearly",
"displayName": "1 Year Protection",
"interval": "annual",
"gatedWorkflowId": "wf-sec-001"
}
],
"canExecuteGatedWorkflow": true,
"serverTime": "2026-10-02T09:30:00.000Z"
}trial.status:not_started,activeorexpired.trialOffer.available: whether this app offers the server trial (withdurationDayswhen it does). Show a "start free trial" action only when it'strueandtrial.statusisnot_started. A free-trial offer configured on the App Store product itself is separate: StoreKit shows it on the product, and the server counts that period as an active plan.availableisfalseunless the tenant sets a trial length for this app.plans: the plans on sale in this app, oldest first. Load them withProduct.products(for:)using eachplatformProductId, and build the paywall from what StoreKit returns.intervalismonthly,annualorone_time_year.entitlement.status:noneif the user has no current plan; otherwiseactive,in_grace_periodorcancelled.cancelledmeans auto-renew is off; the plan still works untilcurrentPeriodEnd.entitlement.inFreeTrial:truewhile a subscription is in the free trial of its introductory offer. Apple decides when it ends and charges; the plan gives access throughout, so you only need this to word the UI.entitlement.platformisapplefor an App Store plan, orreviewerwhen the account was unlocked with the reviewer login (then there is noproductIdorcurrentPeriodEnd). Treat any other value as a label.- Use
canExecuteGatedWorkflowto decide whether to show the paywall. Compare dates againstserverTime, never the device clock.
Starts the user's one server free trial, whose length the tenant sets for this app. This is separate from a subscription's App Store free trial, which needs no call. Calling it again returns the existing trial unchanged, including an expired one, so it's safe to call whenever you're unsure. Send an empty body {}.
Call it only when trialOffer.available is true. Otherwise an account with no trial yet gets 403 with code: "TRIAL_NOT_OFFERED". A trial the user already started, on any device, keeps counting.
If an older version of your app tracked the trial on the device, send that record once as legacyTrial: { startedAt, expiresAt } on the user's first call. The server clamps it to the tenant's trial length, and ignores it if the account already has a trial.
Onboarding and a paywall shown before sign-in can't call entitlement, which needs a session. Call this one at launch with the app's x-api-key and no access token. It returns the same trialOffer and plans as entitlement for this app, and nothing about any account.
{
"trialOffer": { "available": false },
"plans": [
{
"productId": "650f1c2e9b1d4a0012ab34cd",
"platformProductId": "plan_one_year",
"displayName": "1 Year Protection",
"interval": "one_time_year",
"gatedWorkflowId": "wf-sec-001"
}
],
"serverTime": "2026-10-02T09:30:00.000Z"
}trialOffer says whether the app offers a trial, not whether this user can still start one. After sign-in, entitlement is the source of truth. Load the plans with Product.products(for:) using each platformProductId. The response may be cached for five minutes. A key that isn't an Android or iOS app key gets 403.
Purchases
Sign the user in before they buy, so the purchase is tied to their account.
- Buy with
product.purchase()and keep only.verifiedtransactions. - Confirm it with the backend straight away: call
verify-purchasewith the transaction id and product id. - Finish the transaction after a
200withtransaction.finish(). Don't finish it before the backend confirms, or a failed call can't be retried. - Listen for
Transaction.updatesfrom app launch, and on launch also walkTransaction.unfinished. Both catch purchases that completed while the app wasn't running, such as Ask to Buy approvals and renewals. Send each throughverify-purchase; verifying the same transaction twice is safe. - After sign-in, send each plan in
Transaction.currentEntitlementsthroughverify-purchaseonce, so a plan bought earlier is linked to the account.
| Field | Type | Notes |
|---|---|---|
| platformrequired | "apple" | The store the purchase came from. |
| platformProductIdrequired | string | The App Store product id. The server looks it up in the tenant's catalog to decide what was bought. |
| transactionIdrequired | string | String(transaction.id) from StoreKit 2. |
| transferToCurrentAccountoptional | boolean | Only on the retry after the user agreed to move a plan to this account. See Moving a plan. |
{ "kind": "credit_package", "creditsGranted": 500 }{
"kind": "service_entitlement",
"entitlementStatus": "active",
"currentPeriodEnd": "2026-11-02T09:30:00.000Z"
}A plan response also has "transferred": true when the call moved the plan from another account.
| Status | Meaning |
|---|---|
| 400 | Missing transaction id; the transaction belongs to a different app or product; a credit package that isn't consumable; a one-year plan that isn't a non-renewing subscription; or a refunded transaction. |
| 404 | The product id isn't in the tenant's catalog. |
| 409 | PURCHASE_LINKED_TO_ANOTHER_ACCOUNT: this plan belongs to a different account. If error.canTransfer is true, offer to move it (see Moving a plan). Otherwise ask the user to sign in with that account. Finish the transaction either way, since retrying won't change the answer. |
@MainActor
final class PurchaseManager {
private var updatesTask: Task<Void, Never>?
/// Call once at app launch.
func start() {
updatesTask = Task {
for await result in Transaction.updates { await self.handle(result) }
}
Task {
for await result in Transaction.unfinished { await self.handle(result) }
}
}
func buy(_ product: Product) async throws {
if case .success(let result) = try await product.purchase() {
await handle(result)
}
}
private func handle(_ result: VerificationResult<Transaction>) async {
guard case .verified(let transaction) = result else { return } // ignore unverified
do {
try await api.verifyPurchase(
platform: "apple",
platformProductId: transaction.productID,
transactionId: String(transaction.id)
)
await transaction.finish() // only after the backend has granted it
await refreshWalletAndEntitlement()
} catch {
// Leave it unfinished: Transaction.unfinished will offer it again on next launch.
}
}
}You don't need to report these. The App Store notifies the backend directly, and the plan's status updates on its own. Re-read GET /payment/v1/mobile/entitlement when the app returns to the foreground. To restore purchases on a new device, call AppStore.sync() and send the resulting transactions through verify-purchase.
Moving a plan to another account
An account is a phone number or email; a plan is paid for by an Apple ID. When someone signs in with a new number on a phone whose Apple ID already pays for a plan on their old account, verify-purchase answers 409 PURCHASE_LINKED_TO_ANOTHER_ACCOUNT, with error.linkedPlanEndsAt saying when the plan on the other account ends.
- If
error.canTransferistrue, ask the user, for example: "Your Apple ID already has a plan on another account, active until date. Move it to this account? The other account will lose it." - If they agree, call
verify-purchaseagain with the same transaction and"transferToCurrentAccount": true. A200with"transferred": truemeans the plan is now on this account. - If they decline, send nothing more. The plan and its remaining time stay on the other account.
canTransfer is false when the plan has ended or belongs to another workspace; don't offer the move then. The server moves a plan without asking when the old account was deleted or shares this account's phone number or email. A move takes the plan's remaining time with it.
The one-year plan is a non-renewing subscription, so it can be bought again before the current year ends. A year bought on an account that still has time left starts when that time runs out, so no paid day overlaps or is lost. The currentPeriodEnd that verify-purchase and GET /payment/v1/mobile/entitlement return includes the queued year.
Reviewer login
App Review needs the paid feature without paying. Reviewers sign in like anyone else, and the paywall offers a Reviewer login next to the purchase options: a username and password the tenant administrator sets for this app and gives to Apple in App Store Connect under App Review Information. Never put a reviewer credential in the app itself.
{ "username": "app-review", "password": "••••••••" }{
"accessToken": "eyJhbGciOi...",
"refreshToken": "eyJhbGciOi...",
"expiresIn": 1800,
"canExecuteGatedWorkflow": true
}- On
200the account is activated and can run the plan's workflows. Save the new tokens exactly as afterPOST /auth/v1/activate, then re-readGET /payment/v1/mobile/entitlement(it reportsplatform: "reviewer"). 401withcode: "REVIEWER_LOGIN_INVALID"covers a wrong username or password and a login that isn't set up or is turned off. Show one message. Limited to 5 tries a minute and 10 an hour.- Access lasts until the administrator revokes it, which also signs the user out. After signing in again the user sees the paywall.
Store setup
Done once per app by whoever administers the tenant, before purchases can be verified.
- In App Store Connect under Users and Access → Integrations → In-App Purchase, create a key (requires the Admin role). Note the Key ID and Issuer ID, and download the
.p8file. Apple only lets you download it once. - Find the app's numeric Apple ID under App Information. It's different from the bundle id.
- Save the credentials with
PATCH /tenants/v1/:id/apple-config:bundleId,issuerId,keyId,privateKey(the full contents of the.p8file) andappAppleId. - Under App Information → App Store Server Notifications, set both the production and sandbox URLs to
https://<api-host>/webhooks/appleand choose Version 2. Webhooks live outside the/apiprefix; include any deployment path prefix. - Create the catalog: credit packages through
/pricing-config(providerapple, withiapCreditPackages) and plans through/service-entitlement-products(withplatformProductIds.apple). Each product id must match App Store Connect exactly. A plan with intervalmonthlyorannualmust be an auto-renewable subscription;one_time_yearmust be a non-renewing subscription. - Set free trials on subscriptions in App Store Connect, as introductory offers. For a server trial on a one-year plan, use
PATCH /tenants/v1/:id/trial-config:ios.durationDayssets its length, and0(the default) means no server trial. Don't offer both on one app. - Set the reviewer login with
PUT /mobile-apps/v1/reviewer-logins/:apiKeyId(the iOS key's id), and enter the same username and password in App Store Connect under App Review Information.GET /mobile-apps/v1/overviewshows what each app still needs.
08App updates
The tenant can set the oldest build of your app that may keep using the API. When it raises that minimum, older builds get a notice period (24 hours unless the tenant chooses otherwise) in which they keep working and are told to update. After the deadline the API refuses them until the user updates.
- Send
X-App-Buildon every request, signed in or not: yourCFBundleVersion, a whole number such as59. Without it the server can't tell which build is calling. - Check at launch, before sign-in, with
GET /mobile-apps/v1/version-policy. - Read the update headers on every response, and handle
426.
Send the app's x-api-key and X-App-Build. It is never blocked, even for an outdated build.
{
"status": "update_required",
"build": 58,
"minVersionCode": 59,
"latestVersionCode": 60,
"enforceAt": "2026-11-02T10:00:00.000Z",
"storeUrl": "https://play.google.com/store/apps/details?id=com.example.app",
"message": "This update keeps your protection working.",
"serverTime": "2026-11-01T10:00:00.000Z"
}| status | What to show |
|---|---|
| ok | Nothing. |
| update_available | A dismissible "New version available" prompt, using opening storeUrl. Never block. |
| update_required | A banner with the deadline, for example "Update by 2 Nov, 3:30 PM to keep using the app", and an Update button using opening storeUrl. Count down from serverTime to enforceAt, never from the device clock. The app keeps working until then. |
| update_blocked | A full-screen "Update required" with message and a button that opens storeUrl. Don't sign the user out. |
On every other response
| Response | Meaning |
|---|---|
| X-App-Update: available | A newer build exists. Offer it; never block. |
| X-App-Update: required X-App-Update-Deadline: <ISO 8601> X-App-Min-Build: <number> | This build must update before the deadline. The request itself succeeded. Show the deadline banner. |
| 426 | error.code: "APP_UPDATE_REQUIRED", with error.minVersionCode, error.enforceAt, error.storeUrl and error.message. The deadline has passed: show the blocking update screen. Don't retry and don't sign the user out. |
These calls keep working for an outdated build, so the user can always sign out, delete their account or update: the version check, POST /auth/v1/refresh, GET /auth/v1/logout, DELETE /auth/v1/sessions, the account deletion routes (v1 and v2) and the reviewer login.
// Send the build number on every request, from the app and the Safari extension
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "0"
request.setValue(build, forHTTPHeaderField: "X-App-Build")
// After every response
if let http = response as? HTTPURLResponse {
switch http.value(forHTTPHeaderField: "X-App-Update") {
case "required": UpdateNotice.showDeadline(http.value(forHTTPHeaderField: "X-App-Update-Deadline"))
case "available": UpdateNotice.offerUpdate()
default: break
}
if http.statusCode == 426 { UpdateNotice.block() } // error.code == "APP_UPDATE_REQUIRED"
}A build only gets the friendly notice if it sends X-App-Build and handles the headers and 426. Builds from before that can only be stopped outright. Add this to the next release, so every later release can be enforced gracefully.
09Errors
Every error uses the same envelope. Branch on the HTTP status first, then on error.code when it's present.
{
"statusCode": 403,
"timestamp": "2026-10-02T09:30:00.000Z",
"path": "/api/workflows/v1/execute",
"error": {
"code": "ENTITLEMENT_INACTIVE",
"message": "Your trial has ended and no active subscription was found for this service."
}
}| Status | Where | Meaning and what to do |
|---|---|---|
| 400 | Any | A field failed validation. error.message lists the problems. A file problem has an error.code from the table below. On execute it can also mean the workflow rejected the input: error.message is The workflow could not process this input, with error.detail (a short reason) or error.fields ([{ field, issue }]) when available. Ask the user to change the input; retrying the same input fails again. |
| 413 | execute | PAYLOAD_TOO_LARGE: the upload is over the server's size ceiling. Show error.message. |
| 401 | Any authenticated call | Token expired or session ended. Refresh once; if that fails, use error.code to explain and return to sign-in. |
| 403 | execute | Insufficient credit balance: offer a credit package. ENTITLEMENT_INACTIVE: show the plan paywall. |
| 404 | execute, reports | Unknown or unavailable workflow, or a failed or missing report. |
| 409 | execute | The same workflow with identical input is still running from a moment ago. Wait for it instead of retrying. Only returned for workflows that cost credits and for report workflows. |
| 401 | reviewer-unlock | REVIEWER_LOGIN_INVALID: the reviewer details are wrong or not set up. Stay on the paywall; don't sign out. |
| 429 | Sign-in, refresh, reviewer-unlock | Rate limited. Back off. |
| 426 | Any app call | APP_UPDATE_REQUIRED: this build is no longer supported. Show the blocking update screen (see App updates). Don't retry or sign out. |
| 502 | execute | The workflow service failed or couldn't be reached (Workflow service unavailable. Try again later.). Safe to retry with backoff. |
| 5xx | Any | Temporary. Retry with backoff. |
File errors on execute are 400. Show error.message as-is; {label} is the input's own label and the limits come from its fileOptions.
| error.code | error.message |
|---|---|
| FILE_REQUIRED | {label} is required |
| FILE_EMPTY | The selected file is empty |
| FILE_TYPE_NOT_ALLOWED | {label} must be one of: .mp4, .webm, .mov, .avi (the input's accepted formats) |
| FILE_UNREADABLE | The selected file is corrupt or could not be read |
| FILE_TOO_LARGE | {label} exceeds the maximum size of 100MB (the input's maxSizeMb) |
| FILE_COUNT_EXCEEDED | {label} does not accept multiple files |
10Launch checklist
- The
IOSAPI key comes from build configuration and is never logged. - Tokens live in the Keychain (in a shared access group if an extension uses them), and every refresh saves the new pair before it's used.
- Refresh goes through one actor, and 401 handling reads
error.codeto show the right sign-out message. - File inputs read
fileOptionsfrom the workflow instead of hardcoding limits, execute uses multipart, and file errors showerror.messageas-is. - Report runs use the realtime channel with a polling fallback that keeps going for at least 20 minutes.
- Credit packages are Consumable products. Transactions are finished only after
verify-purchasesucceeds, andTransaction.updatesandTransaction.unfinishedare handled from launch. - The paywall is driven by
canExecuteGatedWorkflow,plansandtrialOfferfrom the server, andserverTime, not the device clock. - Every execute call, from the app and the Safari extension, sends
X-App-Version. 409 PURCHASE_LINKED_TO_ANOTHER_ACCOUNTwithcanTransfer: trueoffers to move the plan, and only retries withtransferToCurrentAccountafter the user agrees.- Every request sends
X-App-Build; the app checksGET /mobile-apps/v1/version-policyat launch and handlesX-App-Updateheaders and426 APP_UPDATE_REQUIRED. - The paywall's reviewer login calls
reviewer-unlock, and the app contains no reviewer credential of its own. - In-app account deletion asks the person to confirm who they are and calls
DELETE /users/v2/account.