Backend Event Integration
To send events to the Scrimmage API and reward your users, follow these integration steps.
Node.js Integration
Installation
You can install the @scrimmage/rewards
package using npm, yarn, or pnpm:
npm install -s @scrimmage/rewards
yarn add @scrimmage/rewards
pnpm add @scrimmage/rewards
Initialization
To initialize the library, provide your unique server endpoint and secret key. Obtain the API_SERVER_ENDPOINT
from your admin dashboard URL, which should resemble "your_company_name.apps.scrimmage.co". The secret key is generated during Step 2 of Getting Started.
import Scrimmage from '@scrimmage/rewards';
Scrimmage.initRewarder({
apiServerEndpoint: API_SERVER_ENDPOINT,
privateKey: SECRET_KEY,
namespace: 'production'
});
Keep your secret key safe!
The TS API is the wrapper around the Scrimmage API that allows for a quick integration. It is very important to use this library only on the backend side of the application as it operates with secret keys.
User Authentication
Scrimmage uses an anonymous user ID for each user to maintain their reward profile without storing login credentials. Authenticate users every time they access the reward widget or iframe:
const TOKEN = Scrimmage.user.getUserToken(USER_ID);
Pass this token to the frontend for user authentication. The token is valid for 24 hours.
https://<API_SERVER_ENDPOINT>?token=<TOKEN>
Authenticating a user through API only
If you are only using Scrimmage's API solution and not our frontend, you will use this TOKEN in your API calls instead of passing it to the widget
Sending an Event
To track user activities, use the following methods. Customize the interface as needed:
import Scrimmage from '@scrimmage/rewards';
import { BetExecuted } from '@scrimmage/schemas';
const userId = 'unique id of the user';
// Track a single reward
await Scrimmage.reward.trackRewardable<BetExecuted>(
userId,
'betExecuted', // dataType
{
betType: 'single',
isLive: false,
odds: 1.5,
description: 'lorem ipsum',
wagerAmount: 100,
netProfit: 50,
outcome: 'win',
betDate: Date.now(),
bets: [
{
type: 'spread',
odds: 1.5,
teamBetOn: 'team a',
teamBetAgainst: 'team b',
league: 'nba',
sport: 'basketball',
},
],
}
);
// Track a reward once
await Scrimmage.reward.tranRewardableOnce<BetExecuted>(
userId,
'betExecuted', // dataType
'unique id of the reward',
{
betType: 'single',
// ... other data
}
);
Please insert this code wherever your bets (events) are executed. Once this code is inserted, it will open up a one-way connection for you to send bet details to Scrimmage.
Avoiding Duplicates
Scrimmage API ignores duplicate events with the same unique ID, preventing multiple rewards for the same action.
API Event Integration
Integrate the rewards system with your application through two endpoints:
Register a User and Get a User Token
Refer to the Create a user if not exists endpoint in the API documentation:
curl -X POST \
https://<your-org-id>.apps.scrimmage.co/api/integrations/users \
-H 'Authorization: Token <your-secret-key>' \
-H 'Scrimmage-Namespace: <your-namespace>' \
-H 'Content-Type: application/json' \
-d '{ "id": "<user-id>" }'
Send an Event for the User
See the Record a rewardable event endpoint in the API documentation:
curl -X POST \
https://<your-org-id>.apps.scrimmage.co/api/integrations/rewards \
-H 'Authorization: Token <your-secret-key>' \
-H 'Scrimmage-Namespace: <your-namespace>' \
-H 'Content-Type: application/json' \
-d '{
"userId": "<user-id>",
"dataType": "<event-type>",
"body": {
"property1": "value1"
}
}'
To ensure a specific event is rewarded only once per user, include an eventId
in your request:
curl -X POST \
https://<your-org-id>.apps.scrimmage.co/api/integrations/rewards \
-H 'Authorization: Token <your-secret-key>' \
-H 'Scrimmage-Namespace: <your-namespace>' \
-H 'Content-Type: application/json' \
-d '{
"userId": "<user-id>",
"eventId": "<event-id>",
"dataType": "<event-type>",
"body": {
"property1": "value1"
}
}'
Updated 8 months ago