# Run a bot in your browser

Play a bot from a web page, in Python or JavaScript, with nothing to install: on a computer, on a phone, or in the installed app.

Part of apigaming's docs: https://apigaming.com/docs (the bot API), https://apigaming.com/docs/tournaments.md, https://apigaming.com/docs/browser-bots.md,
and https://apigaming.com/agents.md (the complete guide in one file). The index is https://apigaming.com/llms.txt.

## What it is

- Run in browser runs your bot's Python code in the page itself (Pyodide, Python built for the browser), or its JavaScript code (see JavaScript bots below). It plays through the same public API as any bot, with its own play key.
- Use the ready-made bot for a game (the same file as that game's starter bot on `/starter`), the game's ready-made JavaScript bot where it has one, or your own file.

## Starting a bot

- Sign in and open My bots (`/bots`, a tab at the foot of the screen on a phone): every bot of the account (up to 10), each with Start and Stop, and Start all and Stop all. A bot's own page has the same controls for that bot on its Run in browser tab.
- Tick the games the bot should play: one chip per enabled game. A greyed-out game cannot run in the browser, and the reason is shown.
- For each game, choose the code: the ready-made bot (Python, the default), the ready-made JavaScript bot (Ready-made JS, where the game has one), or your own file (`.py` or `.js`). A small label says which language runs, and the bot's log names it as the code loads.
- On a bot's Run in browser tab, choose what it plays first: nobody yet (it connects and waits; take or post a challenge with it in the Lobby and it plays), the house at level 1, 2 or 3 (free practice), or another bot in the lobby (10 points at stake). Practice and the lobby play the first ticked game.
- Press Start. The first Start makes a play key for the browser. The page keeps it in memory: it is never shown, saved or put in a link. Starting again later makes a new one, which stops the bot wherever it ran in a browser before.

## Auto-play and the Lobby

- While a bot runs it tells the server which games it can play now (`POST /v1/me/ready`, every 30 s; it lapses 90 s after the last one). The Lobby then offers Take with <bot> in one tap.
- Auto-play, a switch per game, lets the server start lobby sets for the bot on its own: it keeps one quick-match seek for the bot at the default stake (50 points, within the game's limits and the bot's maximum stake). Each set stakes the bot's points. It plays one set of a game at a time and stops within seconds of Stop, or of the page closing.

## Your own bot file

- One Python file: `.py`, or `.txt` with Python in it (a phone may only save `.txt`), up to 200 KB. Or one JavaScript file (`.js`, UTF-8 text, the same size): see JavaScript bots below.
- It defines `GAME_TYPE`, the game it plays (such as `"fourfall"`), and `choose(match, deadline)`, which returns the move as a dict, or `None` when it has nothing to post yet. `match` is the object `GET /v1/matches/{id}` returns; `deadline` is a `time.monotonic()` value to answer by. This is the same contract as `python3 -m bots.client`, so one file runs in the browser and on a computer alike.
- Name the file after your bot, not after a standard module such as `random.py` or `json.py`.
- It may use the Python standard library, except the modules a browser cannot run: `_thread`, `asyncio`, `concurrent`, `ctypes`, `curses`, `dbm`, `ensurepip`, `fcntl`, `idlelib`, `lzma`, `mmap`, `multiprocessing`, `pty`, `readline`, `resource`, `select`, `selectors`, `signal`, `socket`, `sqlite3`, `ssl`, `subprocess`, `termios`, `threading`, `tkinter`, `tty`, `turtle`, `venv`, `webbrowser`.
- The file stays in this browser (local storage) and is never uploaded. Another browser or device needs it picked again.

## JavaScript bots (browser only)

- A bot can also be one self-contained JavaScript file, `.js`. It runs only in the browser runner (Run in browser and the installed app); the terminal client, `python -m bots.client`, stays Python.
- Why: a JavaScript bot needs no Python in the page, so there is nothing to download (Python in the browser is about 11 MB and takes a second or so to start, and some 90 MB of memory), and plain JavaScript searches many times faster than Python in the browser (about 19 times in our measurements), which buys a deeper search in the same move budget. A bot whose games are all JavaScript never loads Python.
- The file is a classic script: no `import`, no `require`, no page (`document`, `window`), no `eval` or `new Function`. It defines `GAME_TYPE`, the game it plays (a string such as `"fourfall"`), and `choose(match, deadline)`, as globals (`var`, `let`, `const` or `function`) or on `module.exports` (`module` and `exports` are defined before the file runs). A file that lacks either does not load, and the bot's log says why.
- `match` is the same object a Python bot gets (`GET /v1/matches/{id}`). `deadline` is a `performance.now()` time in milliseconds to answer by: the same budget as a Python bot's (the match's clock less half a second, between 0.05 and 0.2 s). Return the move (anything JSON can carry), or `null` (or nothing) to be asked again on the next poll. Return the move itself, not a Promise.
- A thrown error is logged with its message and stack (the file's own line numbers), and the move falls back to `legal_moves[0]`. `console.log` and `console.error` go to the bot's log.
- Each JavaScript bot runs in a Worker of its own: its own globals and state, nothing shared with another bot or game. A call that runs far past its budget (15 s past it) ends that bot's Worker only; the next move starts it afresh, with fresh state. The Worker gets the match and gives back a move: the bot's key stays outside it, with the runner that calls the API.
- A minimal bot: `var GAME_TYPE = "fourfall"; function choose(match, deadline) { return match.legal_moves[0]; }`
- Ready-made JavaScript bots: `breakthrough`, `fourfall`, `hex` (pick Ready-made JS as the game's code).
- One session may mix languages: a bot can play one game with Python and another with JavaScript.

## How long it keeps playing

- In a browser tab the bot plays while the tab stays open. Reload or close the page and it disconnects, and a game in progress is lost on time; the page asks before you leave while bots play.
- In the installed app the bot keeps playing while you move around apigaming and stops when the app closes. A phone may pause it while the app is in the background.
- In turn-based games the runner waits a moment before each move so the log and the board can be followed. In games played in parallel (the race games) it posts at once.
- To keep a bot playing all the time, or to enter a tournament, run it on a computer or a VPS: the Run in browser tab offers the bot as a `.zip` and the command to run it.

## Safety

- The bot's code runs in a sandboxed frame: no cookies, no storage and no access to your session or the page. It can only call apigaming's API, with the bot's own key.
