> For the complete documentation index, see [llms.txt](https://docs.2bttns.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.2bttns.com/getting-started/quick-start.md).

# Quick Start

Create personalized content feeds, marketplaces, social networks, and more with just a few lines of code.

## **Prerequisites**

Ensure you have the following installed in your environment:

* [2bttns CLI](/references/command-line-interface-cli.md)
* [Docker / Docker Compose](https://docs.docker.com/compose/install/)&#x20;

***

#### **Windows**

{% hint style="success" %}
If you are using macOS or Linux, skip to the setup steps.
{% endhint %}

1. Ensure Docker Desktop is installed and configured with the WSL 2 backend. [Follow the instructions here](https://docs.docker.com/desktop/wsl).
2. Use a WSL terminal for the following commands. Open it using `wsl.exe` in your Windows Command Prompt/PowerShell.
3. Once that's done, continue with the setup method below.

***

## Step 1: Launch Console

{% hint style="info" %}
If you want to test games in the cloud, it's best to start by [putting your Console in the cloud.](https://www.google.com)
{% endhint %}

`2bttns` is a containerized application, seamlessly running in the background through Docker.&#x20;

When you launch [2bttns/2bttns](https://hub.docker.com/r/2bttns/2bttns), you gain access to the Console: a built-in admin panel for creating and hosting interactive games. The Console makes it easy to build one or multiple apps through one dashboard, allowing you to manage data, create games, and much more, all with just a couple of clicks. Follow these steps to quickly set up your development environment using 2bttns.

### **Create using `new`**

With the CLI installed, you can now create a new console. Follow the steps to configure your Console with your `DATABASE_URL`. As of now, 2bttns **exclusively supports PostgreSQL database**.&#x20;

&#x20;In your terminal, execute:

{% hint style="warning" %}
Make sure Docker is running!🐳
{% endhint %}

```bash
2bttns-cli new
```

Behind the scenes, this will:

* create a `docker-compose.yml` file in the current directory.&#x20;
* launch your Console,&#x20;
* apply migrations to your specified database,&#x20;
* seed the database with examples (optional)

<figure><img src="https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/7cRY7M8b3K2gTEtwre6c/Screenshot%202024-02-23%20at%2011.48.14%E2%80%AFPM.png" alt=""><figcaption><p>Your Console is successfully set up and running behind the scenes using Docker.</p></figcaption></figure>

### &#x20;**Create admin account**&#x20;

The next step involves setting up an admin account to access your Console securely.

<figure><img src="https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/Cr0XPE2ArIVqZRPeEtRL/Screenshot%202024-02-23%20at%2011.49.31%E2%80%AFPM.png" alt="" width="563"><figcaption><p>Create an admin user via the 2bttns CLI (included in docker container)</p></figcaption></figure>

```bash
docker compose exec twobttns 2bttns-cli admin create
```

**💡Helpful Tip:** We recommend using the Username/Password authentication metho&#x64;**.** You can achieve this through the following command, executed inside your container using the `2bttns-cli` tool:

{% hint style="success" %}
**You're all set! 🎉**&#x20;

To get started, open your Console at <http://localhost:3262/auth/signIn?callbackUrl=/>&#x20;

With your Console setup, you're ready to upload data, develop, and host your games.&#x20;
{% endhint %}

<figure><img src="https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/Gb1XiPMh1vG4Sjcg3DzD/Screenshot%202024-02-23%20at%2011.58.14%E2%80%AFPM.png" alt=""><figcaption><p>Open your Console at localhost:3262 and log in.</p></figcaption></figure>

#### **Clean Up**

You can take down the containers using the following command in the same directory as the `docker-compose.yml` file:

```bash
docker-compose down
```

To start your 2bttns/2bttns container up again, run:

```bash
docker-compose up
```

#### Running the Container in the Background

To avoid occupying your terminal window while running the container, include the `--detach` (or `-d`) flag like this:

```bash
docker-compose up -d
```

**Clearing Persisted Data**

If you'd like to clear out the persisted data within your Postgres database without affecting the Docker Volume:

```bash
docker volume rm 2bttns-docker-compose_db-data
```

{% hint style="danger" %}
**Note:** This command removes the volume that stores your database data. Use with caution as this action cannot be undone.
{% endhint %}

#### Environment Variables

<figure><img src="https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/P1dqTAOviMaMymrYzLyD/Screenshot%202024-02-23%20at%2011.53.40%E2%80%AFPM.png" alt=""><figcaption></figcaption></figure>

You can always change these in your `docker-compose.yml`file at any time. These are the environment variables you can configure for your 2bttns admin console.

<table><thead><tr><th width="239">Variable Name</th><th width="334">Description</th><th>Example</th></tr></thead><tbody><tr><td><code>DATABASE_URL</code></td><td>The URL of the Postgres database to connect to.</td><td><code>postgresql://username:password@db-hostname:port/db</code></td></tr><tr><td><code>NEXTAUTH_SECRET</code></td><td>The secret used by NextAuth. You can generate a new secret on the command line with: <code>openssl rand -base64 32</code></td><td><code>placeholder-secret-remember-to-change</code></td></tr><tr><td><code>NEXTAUTH_URL</code></td><td>The URL of the 2bttns app.</td><td><code>http://localhost:3262</code></td></tr><tr><td><code>GITHUB_ID</code></td><td>The GitHub OAuth app ID, if you want to allow admin users to sign in via GitHub.</td><td><code>1234567890</code></td></tr><tr><td><code>GITHUB_SECRET</code></td><td>The GitHub OAuth app secret that corresponds to your <code>GITHUB_ID</code>.</td><td><code>placeholder-secret-remember-to-change</code></td></tr></tbody></table>

{% hint style="info" %}
The image comes pre-configured with a Postgres database for an immediate start with 2bttns.\
\
For those preferring their own database, 2bttns automatically sets up necessary schemas and tables when connected.
{% endhint %}

***

## Step 2: Using the API

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

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

<figure><img src="https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/0NJtX0ttU9Y87XJamQiI/Screenshot%202024-02-20%20at%209.08.25%E2%80%AFPM.png" alt="" width="375"><figcaption><p>Console > Settings > Apps</p></figcaption></figure>

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.

{% openapi src="/files/kCwNyeiyMRxJvhZ934kk" path="/authentication/token" method="get" %}
[openapi.json](https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/489xqKU5WXWpX6Dz2H47/openapi.json)
{% endopenapi %}

Here's an example fetch request:

{% code title="Example fetch request" overflow="wrap" fullWidth="false" %}

```javascript
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));

```

{% endcode %}

**2. Generate URL to your Game:** Now that you've generated your bearer token, you can use the full RESTful API within the Console.

{% openapi src="/files/kCwNyeiyMRxJvhZ934kk" path="/authentication/generatePlayURL" method="get" %}
[openapi.json](https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/489xqKU5WXWpX6Dz2H47/openapi.json)
{% endopenapi %}

Here's an example where we generate a Play URL using `node-fetch`:

{% code title="Example fetch request" overflow="wrap" %}

```javascript
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", // Use environment variables as needed
        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);
    }
})();

```

{% endcode %}

**Getting scored data:** To retrieve scored data for a particular game and user, you can use the `/api/games/getPlayerScores` endpoint and pass in the `game_id` and `player_id`.

{% openapi src="/files/kCwNyeiyMRxJvhZ934kk" path="/games/getPlayerScores" method="get" %}
[openapi.json](https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/489xqKU5WXWpX6Dz2H47/openapi.json)
{% endopenapi %}

You can also retrieve scored data for a particular, across all Game Objects and Games:

{% openapi src="/files/kCwNyeiyMRxJvhZ934kk" path="/game-objects/ranked" method="get" %}
[openapi.json](https://content.gitbook.com/content/n2L7ltGlCAKlpbJZ3edj/blobs/489xqKU5WXWpX6Dz2H47/openapi.json)
{% endopenapi %}

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

## Next Steps

* [API Reference](/references/apis.md)
* [Building games](/how-to/build-a-game.md)
* [Redirect user to games](/how-to/integrate-game-via-api.md)
* [Creating and managing data](/how-to/manage-game-data.md)
* [Getting user data](/how-to/retrieve-scores-via-api.md)
