Skip to main content

Token Server

Your access key is your unique authentication key to be used to generate room tokens for accessing the 4Players ODIN server network. Think of it as your individual username and password combination all wrapped up into a single non-comprehendable string of characters, and treat it with the same respect.

info

You can create an access key for up to 25 users below for free and without registration. Please contact us if you want to go into production or need more.

ODIN Access Key Generator

Click on the button to create an access key that you can use for free for up to 25 concurrent users.

While you can create an unlimited number of access keys for your projects, we strongly recommend that you never put an access key in your client code. The following Node.js server application starts a basic HTTP server and utilizes our @4players/odin-tokens package to generate a room token to be used by your client to access a room.

Node.js Token Server​

This is a simple Node.js server application that generates room tokens for your clients to access the 4Players ODIN. It requires Node.js 22.12 or newer. The source code of the package, including a Deno version of this example, can be found on GitHub.

Create a new project and install the package:

npm init -y
npm pkg set type=module
npm install @4players/odin-tokens

Store your access key in a .env file next to the server, so it never ends up in your source code:

.env
ODIN_ACCESS_KEY=__YOUR_ACCESS_KEY__
server.js
import { createServer } from "node:http";
import { TokenGenerator } from "@4players/odin-tokens";

// your access key from the .env file (see above)
const accessKey = process.env.ODIN_ACCESS_KEY;
if (!accessKey) throw new Error("ODIN_ACCESS_KEY is missing, start with: node --env-file=.env server.js");
const generator = new TokenGenerator(accessKey);
const hostname = "0.0.0.0";
const port = 8080;

createServer(async (req, res) => {
const url = new URL(req.url ?? "/", `http://${req.headers.host ?? hostname}`);
const roomId = url.pathname.slice(1) || "default";
const userId = url.searchParams.get("user_id") ?? "unknown";
const token = await generator.createToken(roomId, userId);
console.log(`💡 new token for '${userId}' in '${roomId}'`);
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ token }));
}).listen(port, hostname, () => {
console.log(`🚀 on http://localhost:${port}/my_room?user_id=john`);
});

Start the server and open the URL it prints to get a token for the user john in the room my_room:

node --env-file=.env server.js
warning

Since version 2.0 of @4players/odin-tokens, createToken() is asynchronous and returns a Promise. Make sure to await it. Otherwise, your server responds with "[object Promise]" instead of a token and your clients fail to join the room.