The Attribution API sends app installs, reinstalls, and opens to Branch from your own servers, and returns the deep link data for each session. A Branch SDK does this for you by default, so use an SDK for any app that can run one. Every request and response field is in the API reference.
When you use this API instead of an SDK, session handling becomes your job. You need to handle poor or missing connectivity, save data on the device, get values like the IP address, track downstream conversions, and create shareable links.
What you can do
- Record an app session: Send one install, reinstall or open, and get the session's deep link data back. You can also try it from the reference. With your real Branch Key, Try it sends a real request, so Branch records the session in your data.
Before you begin
This API doesn't use the shared credentials, errors and limits on the API landing page. Its own are on this page.
| Operation | Credential | Where to send it |
|---|---|---|
| Record an app session | Branch Key and Branch Secret | In the request body, as branch_key and branch_secret. |
Send your Branch Key, or your app_id instead, in every request. Send your Branch Secret too whenever server_to_server is true. Branch checks the secret only then, and rejects the request if it's missing or wrong. This API doesn't use an Access Token. To find your Branch Key and Branch Secret, see Credentials.
Access
To use these features, you first need Branch to enable them for your app. Contact Branch Support with your Branch app ID to ask for them:
X-IP-Overrideheader: Lets your server send the device's IP address.- Server-to-server identity: Lets you use
server_to_server_identityandidentity. - Requests handled in the European Union (EU): Otherwise Branch returns a 403. Contact your Customer Success Manager for this one.
Errors
Every error body looks like {"error": {...}}. A request that fails validation names each invalid field, for example {"error": {"os_version": "doesn't pass regex"}}.
| Status | Message | Cause and fix |
|---|---|---|
| 400 | Invalid or missing app id, | The Branch Key, app_id or Branch Secret is missing or wrong. Branch checks the secret only when server_to_server is true. |
| 400 | A field name, with missing required field, not a valid option, doesn't pass regex or too small | A field failed validation. Fix the named field. See Operating system values. |
| 400 | Requests with first_session must also have server_to_server: true. | You set first_session to true without setting server_to_server to true. Branch returns this error alone. |
| 400 | Invalid JSON | Branch couldn't read the body as JSON or as a form. Some malformed bodies return the credentials error instead. |
| 403 | Authentication completed successfully, | Branch's EU data center received the request and your app isn't enabled for it. Contact your Customer Success Manager. |
| 413 | Request body is too large | The body is over 100 KB. |
| 500 | Internal Server Error | Branch couldn't process the request. Send it again. |
How it works
Each section covers a behavior to know before you build.
Send a request for each session
Send a request to POST https://api2.branch.io/v1/open once you know the user has brought your app to the foreground. A session starts when your app launches from a cold start, or returns from the iOS or Android task manager to the foreground. Send the body as JSON.
Read the response
Branch returns 200 whether the session came from a Branch Link. data is a JSON-encoded string, so parse it before you read it. A session from a Branch Link has +clicked_branch_link set to true, plus the link's own data such as $canonical_url and $deeplink_path. Use +clicked_branch_link to decide whether to use Branch routing or your own. If a non-Branch URL opened the app and you sent it, +non_branch_link holds it.
Decide install, reinstall, or open
Branch records the event type for you. Set server_to_server to true and first_session to true on the user's first session. Branch then records an install if it hasn't seen the app on this device before, and a reinstall if it has. With first_session set to false, Branch records an open. If you leave first_session out, Branch records an install if it hasn't seen the app on the device and an open otherwise, and never a reinstall.
Send the device's IP address
Branch reads the device's IP address from the request. When your server sends the request, that's your server's IP. Ask Branch Support to enable the X-IP-Override header for your app, then send the device's public IP in it. For apps that aren't enabled, Branch ignores the header, and returns no error. Also send local_ip when you have it.
Send the device token back
The first response holds either randomized_device_token or device_fingerprint_id, never both. Send the value back under the same name on every later request for that device. Branch returns randomized_device_token when your request included a randomized token, or an sdk value from an SDK version that supports them. Otherwise it returns device_fingerprint_id. Both are 1 to 19 digits.
Set a custom user ID
For server-to-server requests, send server_to_server_identity set to true and identity to get an identity back. Branch returns it as identity_id or randomized_bundle_token, using the same rule as the device token. Send it back on every later request so Branch connects the device's events to the same identity. Send only an ID that holds no personal information, and encrypt it first. See Best Practices to Minimize Data Sent to Branch.
Operating system values
Branch compares os exactly, so use iOS, Android, mac_OS, or tv_OS, not IOS, ANDROID, MAC_OS, or TV_OS. The reference lists every accepted value.
For iOS, mac_OS, tv_OS and watch_OS, os_version must have a major and a minor part, such as 17.3 or 17.3.1. A value such as NA or 17 returns {"error": {"os_version": "doesn't pass regex"}}. Every other os accepts any os_version.
Send each field as its documented type
Most fields must be the exact JSON type that the reference shows. For example, send screen_height, screen_width, and retryNumber as numbers, every advertising_ids value as a string, and launch_options as an object. A field with the wrong type stops Branch from reading the body at that field. Branch then loses the fields that come after it, or returns Invalid JSON or the credentials error. These fields accept a string, a number, or a Boolean: app_id, os_version, the Boolean fields, clicked_referrer_ts, install_begin_ts, and the token and ID fields (randomized_bundle_token, identity_id, randomized_device_token, and device_fingerprint_id).
Collect launch data
Collect these values in your app and send them with each request.
Collect iOS launch data
Add your URI scheme to your Info.plist file, and add your app.link and alternate.app.link domains to your project's Associated Domains. Then collect these values:
- The device ID, which is the IDFA if available, otherwise the IDFV.
- The device's user agent string.
- The URI that opened the app, from your AppDelegate's
openURL()function. If it has alink_click_idquery parameter, send its value aslink_identifier. Forexample://open?link_click_id=1234, send1234. - The web URL that opened the app, from the
webpageURLof theNSUserActivityin your AppDelegate'scontinueUserActivity()function. Send the whole URL asuniversal_link_url, whether it's a Branch Link.
Support Apple Ads
(Optional) For server-to-server integrations, send the attribution token as apple_attribution_token on install. To get it, import the AdServices framework and request the token.
func appleAttributionToken() -> String? {
if #available(iOS 14.3, *) {
return try? AAAttribution.attributionToken()
}
return nil
}+ (NSString *)appleAttributionToken {
#if !TARGET_OS_TV
if (@available(iOS 14.3, *)) {
NSError *error;
NSString *appleAttributionToken = [AAAttribution attributionTokenWithError:&error];
if (!error) {
return appleAttributionToken;
}
}
#endif
return nil;
}The token expires after 24 hours, so send it to Branch within that time. See Apple's documentation and our Apple Ads guide.
Collect Android launch data
Add your URI scheme to your AndroidManifest.xml file, and add your app.link and alternate.app.link domains to it as well, as in the Android App Links setup. Then collect these values:
- The device ID, which is the GAID if available, otherwise the Android ID.
- The device's user agent string. Also send it in the request's
User-Agentheader. - The URI that opened your
MainActivity, from its intent. If it has alink_click_idquery parameter, send its value aslink_identifier. - The web URL that opened your
MainActivity, from the same intent. Send the whole URL asandroid_app_link_url, whether it's a Branch Link.
Support Google Play Install Referrer
(Optional) Do this only in the install session. When the app launches, query the Google Play Install Referrer library for the referrer URL, click timestamp, and install timestamp.
- Send the click timestamp as
clicked_referrer_tsand the install timestamp asinstall_begin_ts. - Look in the referrer URL for a
link_click_idparameter. Send its value aslink_identifier. It can appear as&link_click_id=<>or&link_click_id-<>, so extract only the<>part. - Look in the referrer URL for a
google_search_install_referrerparameter. Send its value asgoogle_search_install_referrer, again extracting only the<>part.
Track other events
Each request to this API records an install, reinstall, or open. To track other events, use the Events API. See Track Branch events for Commerce, Content, Lifecycle, and Custom Events.
Limits
This API has no request rate limit of its own, and it never returns a 429. Branch applies these limits instead.
- Request size: 100 KB for the whole body.
- Repeat opens: Branch can record only one open from a device within a window of time, which Branch sets per app. The default window is four hours. Branch doesn't apply it to opens from a Branch Link, or when your request has
universal_link_url,android_app_link_url,external_intent_uriorsearch_ad_encoded, or hasdebugset totrue. A repeat open still gets a normal response, but it doesn't appear in your reports or exports. - Sessions Branch doesn't record: Branch returns deep link data but records nothing when
tracking_disabledistrue,cpp_levelisNONE, orretryNumberisn't0.
Examples
These examples show common tasks from start to finish.
Record the first session on iOS
Send an install from your server, with the device's IP address.
curl -X POST "https://api2.branch.io/v1/open" \
-H "Content-Type: application/json" \
-H "X-IP-Override: 203.0.113.42" \
-d '{
"branch_key": "key_live_xxxx",
"branch_secret": "secret_live_xxxx",
"server_to_server": true,
"first_session": true,
"os": "iOS",
"os_version": "17.3.1",
"app_version": "1.12.4",
"model": "iPhone15,2",
"user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"hardware_id": "8C2A4C3B-1D5E-4F6A-9B7C-0D1E2F3A4B5C",
"hardware_id_type": "idfa",
"is_hardware_id_real": true,
"ad_tracking_enabled": true,
"screen_height": 2556,
"screen_width": 1179
}'The response holds the session and the link's data. Save device_fingerprint_id and send it on this device's next request. See Record an app session.
{
"session_id": "429691081177874743",
"device_fingerprint_id": "823600311503935224",
"data": "{\"+clicked_branch_link\":true,\"+is_first_session\":true,\"+match_guaranteed\":true,\"~channel\":\"facebook\",\"$canonical_url\":\"https://example.com/home\"}"
}Record the next session
Send an open for the same device, with the token from the first response.
curl -X POST "https://api2.branch.io/v1/open" \
-H "Content-Type: application/json" \
-d '{
"branch_key": "key_live_xxxx",
"branch_secret": "secret_live_xxxx",
"server_to_server": true,
"first_session": false,
"os": "iOS",
"os_version": "17.3.1",
"user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"device_fingerprint_id": "823600311503935224"
}'The response has no link data, so +clicked_branch_link is false. See Record an app session.
{
"session_id": "823602214081710808",
"device_fingerprint_id": "823600311503935224",
"data": "{\"+clicked_branch_link\":false,\"+is_first_session\":false}"
}