Use the API

Now with your Console running, we can make fetch requests for everything.

Generate a Bearer token to authenticate your fetch requests between your app and your Console.

Console > Settings > Apps

2bttns will use your app_id and secret to your Console to generate a JWT. Navigate to your Console, click Settings, and make sure you're on the Apps tab.

Generate JWT

get

Returns a JSON Web Token (JWT) you can use to authenticate API calls to 2bttns.

You can get the app_id and secret from your 2bttns admin console, under Settings/Apps.

Authorizations
Query parameters
app_idstringRequired
secretstringRequired
expires_instringOptional
Responses
200
Successful response
application/json
Responsestring
get
GET /api/authentication/token?app_id=text&secret=text HTTP/1.1
Host: 
Authorization: Bearer YOUR_SECRET_TOKEN
Accept: */*
text

Here's an example fetch request:

Example fetch request
const fetch = require('node-fetch');

const url = 'http://localhost:3262';
const endpoint = '/api/authentication/token';
const params = {
    app_id: 'your-app-id',
    secret: 'your-secret-value' 
};

fetch(`${url+endpoint}?app_id=${params.app_id}&secret=${encodeURIComponent(params.secret)}`, {
    method: 'GET' 
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

The JWT will remain constant as long as your app_id and secret remain unchanged.

Generate URL to your Game: Now that you've generated your bearer token, you can use the full RESTful API within the Console. Here's an example where we generate a game URL using node-fetch:

Generate play URL

get

Returns a URL you can use to send a user to play a game in 2bttns.

Authorizations
Query parameters
app_idstringRequired

ID of the app you've created in 2bttns

secretstringRequired

Secret of the app you've created in 2bttns

game_idstringRequired

ID of the game you want to play in 2bttns

player_idstringRequired

ID of the player you want to play in 2bttns. If the player doesn't already exist, it will be created.

num_itemsstringOptional
callback_urlstringOptional
expires_instringOptional
Responses
200
Successful response
application/json
Responsestring
get
GET /api/authentication/generatePlayURL?app_id=text&secret=text&game_id=text&player_id=text HTTP/1.1
Host: 
Authorization: Bearer YOUR_SECRET_TOKEN
Accept: */*
text
Example fetch request
let fetch;
(async () => {
    fetch = (await import('node-fetch')).default;

    const url = 'http://localhost:3262';
    const endpoint = '/api/authentication/generatePlayURL';
    const params = {
        app_id: "example-app",
        secret: "example-secret-value",
        game_id: "booksort",
        player_id: "a-user-id",
        callback_url: "https://www.example.com",
        num_items: 5 // num_items will override Round Length set in your Console
    };

    try {
        const response = await fetch(`${url + endpoint}?app_id=${params.app_id}&secret=${encodeURIComponent(params.secret)}&game_id=${params.game_id}&player_id=${params.player_id}&callback_url=${encodeURIComponent(params.callback_url)}`, {
            method: 'GET', 
            headers: {
                'Authorization': `Bearer ${BearerToken}` 
            }
        });
        const url = await response.json();
        console.log(url);
    } catch (error) {
        console.error('Error:', error);
    }
})();

You can use the API to generate games, manage data, and much more.

Next Steps

Last updated

Was this helpful?