Skip to main content

Autosigner API Integration for Game

In this tutorial, we'll integrate the Autosigner API into a game to authenticate users, distribute tokens, post event scores, and check transactions.

Prerequisites​

  • Before proceeding with this tutorial, make sure you have:
  • Basic knowledge of JavaScript for html5 game or web development or C# for Unity game development.
  • Created your own Autosigner service or have access to the provided API URLs.

Steps​

Step 1: Obtain Auth from Web URL​

Before starting the game, we need to retrieve the auth token from the web URL. This token will be used for authentication with the Autosigner API.

const authFromWeb = async () => {
var queryString = window.location.search.substring(1);

if (queryString) {
var queryParams = queryString.split('&');

for (var i = 0; i < queryParams.length; i++) {
var pair = queryParams[i].split('=');

if (pair[0] === 'auth') {
user.auth = pair[1];
console.log('new user:', user.auth);
return user.auth;
}
}
}

return null;
};

Step 2: Trigger Autosigner API to Get Username and UserID​

Once we have obtained the auth token, we'll use it to trigger the Autosigner API to get the username and userID.

Autosigner API Endpoint​

  • To get relevant access to the autosigner API, you need to start the autosigner. Download and initialize the Autosigner file at your local desktop. Refer here to download the autosigner.

  • Two relevant parameters needed for the autosigner:

  • API Endpoint to Get Username and UserID

    • Endpoint: {autosigner_base_url}/users/auth
    • Method: GET
    • Headers:
      • Authorization: Bearer {bearer_token}
      • Content-Type: application/json
  • Below are some code examples that you can follow:

JavaScript(HTML5):

const getUserInfo = async (authToken) => {
const response = await fetch(`${autosigner_base_url}/users/${authToken}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${bearer_token}`
}
});

const data = await response.json();
return data;
};

const authToken = await authFromWeb();
const userInfo = await getUserInfo(authToken);
console.log(userInfo);
  • Replace {autosigner_base_url} with the appropriate base URL for your environment (Testnet or Mainnet), and {bearer_token} with the provided bearer token.

Step 3: Distribute Tokens​

As you have previously created tokens, you can do some actions like distributing the tokens to gamers and other. To distribute tokens, you'll make a POST request to the Autosigner API endpoint.

  • API Endpoint to Distribute Tokens
    • Endpoint: {autosigner_base_url}/transactions/distribute
    • Method: POST
    • Headers:
      • Authorization: Bearer {bearer_token}
      • Content-Type: application/json
    • Query Parameter:
      {
      "TokenId": "your_token_id",
      "Amount": amount,
      "Auth": "your_auth_token_here"
      }
  • Below are some code examples that you can follow:
const distributeTokens = async (token_id, amount, authToken) => {
const response = await fetch(`${autosigner_base_url}/transactions/distribute?TokenId=${token_id}&Amount=${amount}&Auth=${authToken}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${bearer_token}`
}
});

const data = await response.json();
return data;
};

const transactionInfo = await distributeTokens(token_id, amount, authToken);
console.log(transactionInfo);

Step 4: Check Transaction Status​

To check transactions, we'll use the Autosigner API.

  • API Endpoint to Check Transactions

    • Endpoint: {autosigner_base_url}/transactions/{hash}
    • Method: GET
  • Below are some code examples that you can follow:

const checkTransaction = async (hash) => {
const response = await fetch(`${autosigner_base_url}/transactions/${hash}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${bearer_token}`
}
});

const data = await response.json();
return data;
};

const transactionInfo = await checkTransaction(hash);
console.log(transactionInfo);

Step 5: Post Event Score​

To post event scores, make a POST request to the Autosigner API endpoint.

  • API Endpoint to Post Event Scores

    • Endpoint: {autosigner_base_url}/events/score
    • Method: POST
    • Headers:
      • Authorization: Bearer {bearer_token}
      • Content-Type: application/json
    • Query Parameter:
      {
      "score": score,
      "auth": "your_auth_token_here",
      "gameId": "your_game_id"
      }
  • Below are some code examples that you can follow:

const postEventScore = async (score, authToken, gameId) => {
const response = await fetch(`${autosigner_base_url}/events/score?score=${score}&auth=${authToken}&gameId=${gameId}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${bearer_token}`
}
});

const data = await response.json();
return data;
};

const eventScoreInfo = await postEventScore(score, authToken, gameId);
console.log(eventScoreInfo);

Conclusion​

You have successfully integrated the Autosigner API into your game to authenticate users, distribute tokens, post event scores, and check transactions. This allows you to enhance your game with features like token rewards, event tracking, and secure transactions. For more information or more APIs to use, you may refer to Wallet Autosigner Documentation.