Skip to content

Scripting needs Premium

A free account plays through the web client. Its tokens, the account token from password sign-in and the realm token you exchange it for, work only there. Every endpoint that takes a token refuses them anywhere else (a script, a bot, a command line, an agent) with 403 Forbidden and exactly this detail:

{
"detail": "Scripting needs Premium. The tokens of a free account work only in the web client, in a current browser. A script, a bot or an agent needs a premium or creator entitlement and gets its own token through OAuth: see Scripting needs Premium in the docs."
}

That detail is the code to branch on. It is not a 401: nothing is signed out, and the same token keeps working in the web client.

Scripting needs Premium: an API-access entitlement, premium or creator. A script, a bot or an agent then gets its own token through OAuth: you approve it once in your browser, and it receives a token for one realm. Password sign-in is still the web client’s alone: log in, registration and email verification each carry a human check that only a person at the web client’s form can answer.

The OAuth token exchange checks the entitlement. Without one, the browser part of the flow completes and the last step answers access_denied. On a premium or creator account, the account’s own account and realm tokens are accepted outside the web client too.

MCP clients do it for you

An MCP client (Claude Code, Claude Desktop, …) runs this flow by itself the first time it connects: it registers, opens the consent page in your browser and keeps the token. You need nothing on this page for it.

Your own script

The flow is the OAuth 2.1 authorization code grant with PKCE S256 and a loopback redirect. Your script listens on 127.0.0.1, opens the consent page in your browser, and the code comes back to its own port. Every rule the endpoints apply is under For client developers.

1. Register a client, once

Register the redirect URI your script listens on. On a loopback http URI (127.0.0.1, localhost or [::1]) the port is free: the consent page accepts the registered URI on any port, so your script may listen on a fresh port each time. Everything else must be as registered, and the token exchange must name the same URI, port included, as the authorize request.

import httpx
response = httpx.post(
"http://localhost:8731/oauth/register",
json={
"client_name": "My Restfall bot",
"redirect_uris": ["http://127.0.0.1:8765/callback"],
},
)
client_id = response.json()["client_id"]

The answer, 201 Created, holds the client_id. Keep it: every run of your script uses the same one. A new client needs the operator’s approval before the consent page accepts it, so send them the client_id (they run scripts/grant_entitlement.py approve-client).

2. Run the flow

This Python script uses only the standard library and httpx. It makes a fresh PKCE verifier and state for the run, opens the consent page, waits for the one redirect, checks the state and exchanges the code.

import base64
import hashlib
import http.server
import secrets
import urllib.parse
import webbrowser
import httpx
BASE_URL = "http://localhost:8731"
CLIENT_ID = "your-client-id"
REDIRECT_URI = "http://127.0.0.1:8765/callback"
verifier = secrets.token_urlsafe(64)
challenge = (
base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest())
.rstrip(b"=")
.decode()
)
state = secrets.token_urlsafe(16)
answer = {}
class Callback(http.server.BaseHTTPRequestHandler):
def do_GET(self):
query = urllib.parse.parse_qs(urllib.parse.urlsplit(self.path).query)
answer.update({name: values[0] for name, values in query.items()})
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
self.wfile.write(b"Authorized. You can close this tab.")
server = http.server.HTTPServer(("127.0.0.1", 8765), Callback)
webbrowser.open(
f"{BASE_URL}/oauth/authorize?"
+ urllib.parse.urlencode(
{
"client_id": CLIENT_ID,
"redirect_uri": REDIRECT_URI,
"response_type": "code",
"code_challenge": challenge,
"code_challenge_method": "S256",
"state": state,
}
)
)
server.handle_request() # the consent page's redirect, once you approve
server.server_close()
if answer.get("state") != state or "code" not in answer:
raise SystemExit("That was not this run's answer: run the script again.")
response = httpx.post(
f"{BASE_URL}/oauth/token",
data={
"grant_type": "authorization_code",
"code": answer["code"],
"redirect_uri": REDIRECT_URI,
"client_id": CLIENT_ID,
"code_verifier": verifier,
},
)
response.raise_for_status()
grant = response.json()
token = grant["access_token"]
print("Playing realm", grant["scope"], "for", grant["expires_in"], "seconds")

In the browser, the consent page asks you to sign in (with its own human check), pick a realm and authorize the client; a player is created in that realm if you have none. Then it sends the browser to your redirect URI with the code.

A code works for one exchange, within 10 minutes. Exchange it once and never retry an exchange that may have succeeded (a timeout, a dropped connection): run the flow again instead. The exchange’s refusals are listed under Exchanging the code.

3. Play

The token goes in an Authorization: Bearer header on every gameplay endpoint, starting with the game state.

import httpx
state = httpx.get(
"http://localhost:8731/api/v1/state/",
headers={"Authorization": "Bearer your-oauth-token"},
).json()

The token plays the realm you picked and nothing else: what it can do. It lasts 30 days, and there are no refresh tokens: when it expires or is revoked, run step 2 again. To play a second realm, run step 2 again and pick that realm. Its requests count against the per-token rate limit.

On your own development server

A local development server (DEBUG=true, no Turnstile keys) accepts Cloudflare’s test token in place of the widget’s, so a script against it may still sign in with a password: see The human check. The token it gets follows the rule above: on a free account it works only in the web client. Give the account premium or creator first; the operator grants it with scripts/grant_entitlement.py (For administrators).