This article applies if your store connects to ReferralCandy through the API Integration connection type — your systems send purchase information to ReferralCandy through the ReferralCandy API. To compare connection types or find your platform's integration guide, see Integration overview.
The API integration allows you to connect your store to ReferralCandy with greater control and customization options for implementing a referral or affiliate program.
Why API integration
The API integration is the right connection type when:
You can't BCC ReferralCandy on invoice emails, so email integration doesn't work for your store
You can't expose dynamic checkout variables needed for the JavaScript integration
You want to do a custom integration with your existing rewards system
You have a non-standard checkout process for your online store. For example, the invoice information is finalized sometime after the initial JavaScript tracking code is triggered.
Your storefront or checkout is headless or fully custom, so there's no standard purchase completed page for the other connection types to hook into.
Refer to the ReferralCandy API Documentation.
How API Integration works
A referrer signs up by visiting your join page URL. To find this link, on the ReferralCandy dashboard go to Referral campaigns > Select your campaign. The join page URL is at the top of the campaign page, next to Promote your program.
If the customer matches an included segment and does not match an excluded segment, they qualify for enrollment in your live campaign and see their unique referral link. For a static segment, you choose its members in the dashboard or through the API; dynamic segments use the conditions you set.
From another browser and IP address, another person is referred by visiting the referrer's invite link, and then the purchase completed page of your store.
Use the purchase method of the ReferralCandy API to send ReferralCandy purchase information to detect referrals.
You should see referrals detected on your Purchases & Referrals page.
How do campaigns work? See the referral campaign map to learn more.
Set up API Integration
Step 1: Use the purchase method
Connect your store to ReferralCandy using the purchase method.
On the ReferralCandy dashboard, go to Integrations > Standalone.
If your connection type isn't API yet, click Change, select API Integration, and click Save. This only switches which setup instructions the page shows — if another connection method is already running on your store, remove it once the API integration is live, or ReferralCandy may record duplicate purchases.
Before wiring your purchase flow, test your setup with the verify method — a successful response confirms your authentication credentials and signature calculation are correct.
Use the purchase method to send purchase information from your store to ReferralCandy, in particular:
The
order_timestampfield must be within 10 minutes of the visit to your store's purchase completed pageThe
browser_ipanduser_agentfields must correspond with the IP address and browser used by the purchase. See how it workscurl --data "accessID=THISISMYACCESSID&browser_ip=173.255.155.102¤cy_code=USD&email=example@referralcandy.com&external_reference_id=1234&first_name=ReferralCandy&invoice_amount=100.0&last_name=Example&order_timestamp=1581002448×tamp=1581062448&signature=SIGNATUREASCALCULATEDBELOW" --data-urlencode "user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0" https://my.referralcandy.com/api/v2.2/purchase.json
If a discount code was used on the order, the
discount_codefield is required — and friend offer codes help ReferralCandy attribute the referral to the right advocateThe optional
accepts_marketingfield records whether the customer consented to receiving marketing emails — it defaults to true if not sentThe optional
referrer_idfield is a recommended upgrade: it directly identifies the referring advocate for the purchase. By default, ReferralCandy detects referrals by matching signals — the referral-link visit, browser IP, and user agent — whilereferrer_idgives it an explicit answer for who referred the purchase. Usually this is theaicparameter ReferralCandy appends to your store URL when a friend arrives through a referral link: capture it when the friend lands on your store and include it with the purchase.
Make a test purchase at your store.
In the Listening for API request. Make a purchase to send us a signal section, click Next. If you correctly set up the purchase method, this section shows a green checkmark.
Step 2: Add the referral tracking code to your store
On the ReferralCandy dashboard, go to Integrations > Standalone. The tracking code is in the Add ReferralCandy to your online store section — click Next on the listening step if it isn't visible yet.
Copy the code snippet. The code is unique to your account, so always copy it from your own dashboard.
Paste the code just before the closing
</body>tag on your store's purchase completed page.
For the full walkthrough of this step — including how to confirm the code is being detected — see Adding the referral tracking code.
Tips for ReferralCandy API integration
Provide all required fields for a particular API method and include them in the signature calculation. See a sample
If you provide non-required fields, make sure that they are also included as part of the signature calculation
Sort all the name and value pairs in alphabetical order
If you have non-required fields, sort them in alphabetical order
Use the correct values for the access ID and secret key. On the ReferralCandy dashboard, go to Account > Profile — your API tokens (API Access ID and API Secret ID) are in the API Tokens section.
Beyond purchases: other API methods
The purchase method covers the core store connection, but the ReferralCandy API also has methods for managing contacts, referrals, and rewards programmatically:
Verify — test that your authentication credentials and signature calculation are set up correctly
Campaigns — list the campaigns on your account
Signup — register a contact and attempt enrollment in your campaigns programmatically. For a new contact, pass
segment_idto add them to a static segment before enrollment is checked. Check the response’ssegment.assignedand per-campaign enrollment results; creating a contact does not guarantee enrollment. The response includes the referral link and portal sharing page for successful enrollmentsSegments — list the static segments in your account and their IDs. Dynamic segments are not included
Add Segment Contact — add an existing contact to a static segment using their email address and
segment_id, then check enrollment across your campaigns. The contact must already exist; use Signup withsegment_idfor a new contactRemove Segment Contact — remove a contact from a static segment using their email address and
segment_id, then check enrollment again. A contact who no longer matches a live campaign’s target audience is unenrolled. They stay enrolled if they still qualify through another included segment, such as All Customers, and do not match an excluded segmentInvite — send the welcome email to a contact enrolled in a campaign
Referral — update the status of a referred purchase, for example to report a return so the advocate isn't rewarded for it
Referrals, Referrer, and Contacts — query referred purchases, look up who referred a particular customer, and list your contacts
Unsubscribed — unsubscribe or resubscribe a contact from your referral program emails
Rewards methods — query and update custom rewards, used when accumulating referral rewards in your own platform
The Segments, Add Segment Contact and Remove Segment Contact methods require API v2.2. Create the static segment and configure each campaign’s included and excluded segments in the dashboard. Adding someone to a static segment does not override exclusions or guarantee enrollment. A shared segment can affect several campaigns.
For Add Segment Contact and Remove Segment Contact, inspect the response’s enrollments array as well as the HTTP status. Paused and stopped campaigns are not checked again: their results contain CAMPAIGN_PAUSED or CAMPAIGN_STOPPED, and enrollment in those campaigns stays unchanged.
See the API Documentation for each method's parameters, responses, and examples.
Testing and troubleshooting
To ensure that the referral tracking code works, make a test purchase in your store and go to your Purchases & Referrals page to verify that purchases are detected. You can also confirm the tracking code itself is being seen: on the Standalone page, Last detected URL under the tracking code shows the page where the code was last detected.
A purchase can be recorded without being marked as a referral. If that's what you see, the usual causes are qualification rather than the integration — the friend used the same browser or IP address as the advocate, the order was below the minimum purchase amount, or the purchase is still in the review period. See Troubleshooting referral detection.
Common API errors
The ReferralCandy API responds with standard HTTP status codes. The three you're most likely to see while integrating:
401 Unauthorized — check your authentication credentials, signature and API request timestamp. Check your API Access ID and API Secret ID (Account > Profile > API Tokens). An invalid signature or request timestamp can also cause 401: include every sent parameter except
signaturein the signature calculation, sort the fields alphabetically, and use the current UNIX time in seconds fortimestamp. This is the request time, separate from the purchase’sorder_timestamp. After a reset, requests signed with the old secret return 401 once the 7-day allowance expires, or sooner if Revoke previous API Secret was used. Update your integration with the new secret. See Reset your API secret for the steps.400 Bad Request — something in the request is wrong; the response’s
messagefield says what. Common causes include an invalid email, currency code or amount. Add Segment Contact returnsContact not foundif the email is not registered in your account. Add Segment Contact and Remove Segment Contact can returnInvalid segment IDorSegment is not a static segment: use a valid static segment from your account. To register a new contact, use Signup withsegment_idinstead.403 Forbidden — authentication worked, but the method isn't available right now. This can happen when your account is suspended, or when the particular method requires a live campaign and that campaign is paused or stopped — the response's
messageandreasonfields have details. Refer to the campaign states and account states articles to learn how to fix issues related to this error.
Note: Send each order once — a purchase re-sent with the same external_reference_id is deduplicated rather than recorded twice. If a purchase seems missing, check whether it was already sent before sending it again.
Sample signature
Keep your API secret private. Don't share your API Secret ID in support chat, email, screenshots or code samples. The full string before MD5 hashing contains your secret too.
For troubleshooting, replace the secret with YOUR_API_SECRET and remove customer details from any code, request or response you share. Run signature calculations in your own environment with the real secret; use placeholders in shared examples.
If you've already shared your secret, reset it, update the integrations that use it, and revoke the previous secret promptly. Follow the steps in Reset your API secret.
Worked example to calculate signature
Worked example to calculate signature
Here's how you'd calculate the signature for the purchase method.
Gather all the field and value pairs you have, ensuring that all the required fields have a value.
List of fields used in this example to calculate signature
Required
first_name=ReferralCandy
order_timestamp=1581002448
browser_ip=173.255.155.102
user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0
invoice_amount=100.0
currency_code=USD
timestamp=1581062448
accessID=THISISMYACCESSID
Optional
last_name=Example
external_reference_id=1234
Sort the field and value pairs in alphabetical order.
Alphabetical list of fields
accessID=THISISMYACCESSID
browser_ip=173.255.155.102
currency_code=USD
external_reference_id=1234
first_name=ReferralCandy
invoice_amount=100.0
last_name=Example
order_timestamp=1581002448
timestamp=1581062448
user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0
Join all of the field and value pairs into a single string:
accessID=THISISMYACCESSIDbrowser_ip=173.255.155.102currency_code=USDemail=example@referralcandy.comexternal_reference_id=1234first_name=ReferralCandyinvoice_amount=100.0last_name=Exampleorder_timestamp=1581002448timestamp=1581062448user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0
Prepend your API Secret Key to the single string from the previous step:
THISISMYSECRETKEYaccessID=THISISMYACCESSIDbrowser_ip=173.255.155.102currency_code=USDemail=example@referralcandy.comexternal_reference_id=1234first_name=ReferralCandyinvoice_amount=100.0last_name=Exampleorder_timestamp=1581002448timestamp=1581062448user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0
The calculated signature is the MD5 of the combined single string from the previous step:
Digest::MD5.hexdigest("THISISMYSECRETKEYaccessID=THISISMYACCESSIDbrowser_ip=173.255.155.102currency_code=USDemail=example@referralcandy.comexternal_reference_id=1234first_name=ReferralCandyinvoice_amount=100.0last_name=Exampleorder_timestamp=1581002448timestamp=1581062448user_agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0")=> "c4c5c55b142494e8dfcaf4e806900223"
