Backend Implementation

How to implement Code Generation, Verification, & Setting Status in the game backend.

Code Generation, Verification, & Setting Status

Game Backend. During account linking, the game backend should communicate with Pley to generate link codes, verify link codes, and if account linking is successfully executed, set the account link status to true. These three steps should occur in the backend of the game, communicating with Pley through cURL.

Generating Account Link Code

1) Mobile game requests a link code, either when a QR code is scanned or when the Connect Code UI is opened in the mobile game.

2) Use connection-kit/get-link-code to request a generated code from Pley. If a link-code-id is provided (QR code), include it in the request.

3) If no link-code-id is included in the request, Pley will return the code and expiry. Return these to the game and display them to the player (read more).

curl \
-X POST \
--location 'https://api.pley.com/v2/connection-kit/get-link-code' \
--header 'Authorization: Bearer your-org-api-token-here' \
--data '{
    "game_id": "game-id",
    "link_code_id": "link-code-id", // only used with the QR code.
    "payload": "any-payload" // optional payload. Often the game user ID.
}'

Response when generating link code:

{
    "code": "ABC123", 
    "expiry": 1686316953000 // unix timestamp in ms
}

📘

QR-Code Path & Input-Code Path

In the HTTP API, the link_code_id is only used for the QR-code path. No link_code_id is used for the Mobile Game Connect Code UI account linking.

Verifying Account Link Code

Whenever the game starts on the web with an account link code, the game backend should verify the code before executing the linking of users. This should happen whenever the web game loads with a link code parameter (here).

1) Web game requests account linking, providing the link_code (read more).

2) The game backend verifies the link code with Pley using connection-kit/verify-link-code.

3) After successfully getting the game_user_id back from Pley, the game backend can link the accounts. (Backend Account Linking Documentation)

curl \
-X POST \
--location 'https://api.pley.com/v2/connection-kit/verify-link-code' \
--header 'Authorization: Bearer your-org-api-token-here' \
--data '{
    "session_token": "session-token from pley-sdk",
    "code": "ABC123"
}'

Response when verifying link code:

{
    "game_user_id": "xxxxx-xxxx-xxxx-xxxxxxxxxx", // id of the pley user
    "is_guest_user": true,
    "payload": "your-payload" // payload when code was generated
}

Set Account Link Status after Successful Account Linking

After account linking is successfully executed (generation, verification, and the backend/database account linking), inform Pley by setting the account link status to true. This step is important as Pley changes the interface for users with linked accounts to improve their experience.

curl \
-X POST \
--location 'https://api.pley.com/v2/connection-kit/set-accountlink-status' \
--header 'Authorization: Bearer your-org-api-token-here' \
--data '{
    "session_token": "session-token from pley-sdk",
    "linked": true
}'

Response code 200 indicates success.