Secure Tokens
Secure tokens let you personalize campaigns with user data — pre-filling known fields or identifying the participant — in a way that cannot be tampered with by the client. A token is a signed JWT generated by your backend and attached to the campaign.
When a secure token provider is registered, the SDK calls it every time a campaign is
launched or embedded inline and appends the returned token to the campaign URL as the
qual_token query parameter.
Registering a token provider
Use setSecureTokenProvider with an async closure that returns a token for a given
campaign:
import QualifioKit
Qualifio.shared.setSecureTokenProvider { campaign in
// Ask your backend to mint a signed token for the current user.
try await backend.mintToken(for: campaign) // a String, or nil to launch without a token
}
The closure has the following type:
public typealias QualifioSecureTokenGenerator = @Sendable (String) async throws -> String?
| Aspect | Details |
|---|---|
| Argument | campaign — the slug of the campaign about to be launched. |
| Returns | The token String, or nil to launch without a token. |
- Return a
Stringto attach it to the campaign asqual_token. - Return
nilto launch the campaign without a token.
Because the closure is async throws and receives the campaign slug, you can perform
asynchronous work (such as a network request) and mint campaign-specific tokens if your
signing logic depends on the target campaign.
When and how the token is applied
Each time a campaign is launched or an inline campaign starts loading, the SDK:
- Calls your registered provider with the campaign slug.
- If a non-
niltoken is returned, appends it to the campaign URL as?qual_token=<token>. - Opens the campaign in the Safari view controller, or loads it in the inline view.
Error handling
Token generation is bounded by a timeout, so a slow provider cannot block a campaign launch indefinitely.
If your provider throws, times out, or its result is nil, the SDK logs the problem
and launches the campaign without a token. This means a transient token-service failure
degrades gracefully rather than blocking the user.
Security best practices
- Generate tokens on your server, never on the device. The signing secret must never be embedded in your app.
- Keep tokens short-lived. Mint them on demand so each launch uses a fresh token.
- Scope tokens to the current user (and campaign, if applicable).
Next steps
- Campaigns — launch or embed a campaign with the token applied.