Link Search Menu Expand Document

Applying a referral code to an existing user

Contents

Overview

  • Call name: apply-referral-code
  • Endpoint URL: https://api0.spaaza.com/auth/apply-referral-code
  • Request methods: POST
  • Request Content-Type: application/json or application/x-www-form-urlencoded
  • Response Content-Type: application/json
  • Auth required: yes

This endpoint allows a referral code to be applied to an existing user after account creation, rather than at the moment of signup. This is useful in scenarios where a user creates an account first and only later receives or enters a referral code from a friend or other referring user.

In order for this endpoint to work, the chain must have a referral code submission window configured using the alter-chain endpoint (see Chain configuration below). The submission window defines how long after account creation a referral code can still be applied. If the window has expired for a given user, the request will be rejected.

When a referral code is successfully applied, the endpoint creates a referral relation between the referred user and the referring user, and issues any configured rewards (e.g. vouchers) to the referred user. If the referral campaign does not require a purchase by the referred user, the referring user’s reward is also issued immediately.

Chain configuration

Before this endpoint can be used, the chain must be configured with a referral code submission window. This is done by setting two parameters on the chain using the alter-chain endpoint:

  • referral_code_submission_window_unit - The time unit for the submission window. Accepted values are: year, month, week, day, hour, minute, second.
  • referral_code_submission_window_quantity - A positive integer indicating the number of time units.

Both parameters must be set together, or both must be null. For example, setting referral_code_submission_window_unit to day and referral_code_submission_window_quantity to 7 means that a referral code can be applied up to 7 days after the user’s account was created.

These parameters are returned in the chain object in responses to get-chain and get-card.

Version-specific information

The following version-specific changes apply to this endpoint. See the versioning page for more details.

Version Change details
N/A N/A

Permissions and Authentication

This API call requires a valid Spaaza session (see authentication). The session can be as follows:

  • User authentication: the logged-in user applies the referral code to their own account. The user must be an end-user (not an admin user).
  • Admin authentication: the performing user needs to be logged in and have write access to the chain to which the target user is connected. The target user must be identified using the user parameter block and the chain_id parameter.
  • Privileged authentication: the use of privileged authentication is permitted for this endpoint. The target user must be identified using the user parameter block and the chain_id parameter.

HTTP Parameters

The following HTTP parameters can be passed to the API:

Parameter Description
chain_id (integer, conditional) The ID of the chain to which the target user is connected. Required when using admin or privileged authentication.
user (object, conditional) An object identifying the target user. Required when using admin or privileged authentication. The user can be identified by user_id, username, member_number, or authentication_point_identifier. Only one identifier should be supplied.
referral_code (string, mandatory) The referral code of the referring user. This must be a valid referral code belonging to another user in the same chain.
campaign_id (integer, optional) The ID of a specific referral campaign to apply the referral code against. If omitted, the referral code is applied against all currently active referral campaigns on the chain. If supplied, the campaign must be an active referral campaign belonging to the same chain as the target user.

User identifier parameters

When using admin or privileged authentication, the target user is identified using the user object. The following fields can be used inside the user object:

Parameter Description
user_id (integer) The Spaaza ID of the target user.
username (string) The username (email address) of the target user.
member_number (string) The member number of the target user.
authentication_point_identifier (string) The authentication point identifier of the target user in a third-party identity system such as a webstore.

Headers

The following headers can/must be passed to the API call:

Parameter Description
X-Spaaza-MyPrice-App-Hostname (mandatory when using privileged authentication, recommended for user authentication) The hostname of the app which the user is associated with.

Sample requests

User authentication

When authenticated as the target user, only the referral_code (and optionally campaign_id) is required:

{
    "referral_code": "an8it2",
    "campaign_id": 3
}

Admin or privileged authentication

When using admin or privileged authentication, chain_id and user must also be supplied:

{
    "chain_id": 1,
    "user": {
        "user_id": 5
    },
    "referral_code": "an8it2",
    "campaign_id": 3
}

Applying to all active referral campaigns

If campaign_id is omitted, the referral code is applied to all currently active referral campaigns on the chain. A referral relation and reward is created for each active referral campaign:

{
    "referral_code": "an8it2"
}

Sample response

When a single referral campaign is matched, the response includes both a referral_relation object and a referral_relations array:

{
    "result": {
        "code": 1,
        "status": "ok"
    },
    "results": {
        "referral_relation": {
            "id": 1,
            "campaign_id": 3,
            "referring_user": {
                "id": 4,
                "first_name": "Janneke",
                "last_name": "De Bruin",
                "username": "testchainuser@acme.com",
                "member_number": "20000000001",
                "referral_code": "an8it2",
                "authentication_point_identifier": "10055501555",
                "auxiliary_identifier": null
            },
            "referred_user": {
                "id": 5,
                "first_name": "Referred",
                "last_name": "User",
                "username": "referraluser@gmail.com",
                "member_number": "20000000002",
                "referral_code": "4910z3",
                "authentication_point_identifier": "6120897249449",
                "auxiliary_identifier": null
            },
            "redeemed": false,
            "redeemed_date": null,
            "created_date": "2026-03-15 10:30:00"
        },
        "referral_relations": [
            {
                "id": 1,
                "campaign_id": 3,
                "referring_user": {
                    "id": 4,
                    "first_name": "Janneke",
                    "last_name": "De Bruin",
                    "username": "testchainuser@acme.com",
                    "member_number": "20000000001",
                    "referral_code": "an8it2",
                    "authentication_point_identifier": "10055501555",
                    "auxiliary_identifier": null
                },
                "referred_user": {
                    "id": 5,
                    "first_name": "Referred",
                    "last_name": "User",
                    "username": "referraluser@gmail.com",
                    "member_number": "20000000002",
                    "referral_code": "4910z3",
                    "authentication_point_identifier": "6120897249449",
                    "auxiliary_identifier": null
                },
                "redeemed": false,
                "redeemed_date": null,
                "created_date": "2026-03-15 10:30:00"
            }
        ],
        "result_type": "apply-referral-code"
    }
}

When multiple referral campaigns are matched (i.e. campaign_id was not supplied and the chain has more than one active referral campaign), the response includes only the referral_relations array (the singular referral_relation field is omitted):

{
    "result": {
        "code": 1,
        "status": "ok"
    },
    "results": {
        "referral_relations": [
            {
                "id": 1,
                "campaign_id": 3,
                "referring_user": { "..." : "..." },
                "referred_user": { "..." : "..." },
                "redeemed": false,
                "redeemed_date": null,
                "created_date": "2026-03-15 10:30:00"
            },
            {
                "id": 2,
                "campaign_id": 7,
                "referring_user": { "..." : "..." },
                "referred_user": { "..." : "..." },
                "redeemed": false,
                "redeemed_date": null,
                "created_date": "2026-03-15 10:30:00"
            }
        ],
        "result_type": "apply-referral-code"
    }
}

Possible error responses

The following represents a list of possible error responses for the apply-referral-code endpoint:

Code Name and Description HTTP Status Code
3 http_vars_missing
This script is missing some required variables which must be submitted.
400
6 no_valid_session
The user needs to be logged in and a valid session key needs to be sent.
401
264 myprice_app_without_entity
The requested MyPrice app does not have an entity associated with it.
400
265 authorization_invalid
The given authorization header is invalid.
400
267 no_valid_user
A valid username needs to be specified.
401
269 no_myprice_app
MyPrice app is required.
400
270 user_not_found
No user was found matching the supplied parameters.
404
403 campaign_type_not_supported
The supplied campaign is not a referral campaign.
400
405 entity_mismatch
The referring and referred users, or the campaign, do not belong to the same chain.
400
413 campaign_not_found
No active referral campaign was found for the chain or the supplied campaign_id.
400
419 parameter_invalid
A parameter is invalid. This may indicate that the referral code submission window has expired, or that the user is an admin user.
400
480 referral_code_not_found
The referral code was not found.
400
512 existing_referral_code
The user already has a referring user or already has a referral relation for the specified campaign.
400