> ## Documentation Index
> Fetch the complete documentation index at: https://docs.manalens.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Multiplayer and dedicated servers

> Clients track their own player events. The server sends authoritative results, such as a match end or a purchase, with the secret key and the player IDs from the client.

In a multiplayer game, each player's game client tracks that player. A dedicated server is not a player. It sends only the results that it decides, such as a match result or a checked purchase.

## Who sends what

| Where | Key | What it sends |
| - | - | - |
| The game client (Godot, Unity, Unreal) | **Publishable key** (`mana_pk_`) | The player's own events: `game_open`, sessions, `level_start`, settings, errors. |
| Your dedicated server or backend | **Secret key** (`mana_sk_`) | Authoritative results: `match_end`, a checked `purchase`, rewards, bans. |

> [!WARNING]
>
> Never put the secret key in a client build. Anyone can read a key from a game build. A game build uses only the publishable key. The secret key stays on your server, in an environment variable.

## A server build is not a player

The game SDKs know when they run in a dedicated server build:

| Engine | Server build when |
| - | - |
| Godot | `OS.has_feature("dedicated_server")`, or the display server is `headless` (`--headless`) |
| Unity | A player built with the `UNITY_SERVER` define (Dedicated Server build target), or a player started with `-batchmode`. The Unity Editor is never a server build, also in batch mode. |
| Unreal | `IsRunningDedicatedServer()` |

In a server build, the SDK does not send `game_open`, `session_start`, `session_end`, or `app_crash`. So the server does not count as an install, a player session, or a crash. It logs one line that tells you why.

Your own `track()` and `identify()` calls still work. They use the server's own anonymous ID, so they do not join a player. Use them only for events about the server itself, for example `server_start`. Set `build` (for example `"server"`) so you can filter them.

To send the automatic events from a server build anyway, turn on the option:

| Engine | Option |
| - | - |
| Godot | `"track_server_builds": true` in `init()`, or **Track Server Builds** in **Project Settings > Manalens** (Advanced Settings) |
| Unity | `TrackServerBuilds = true` in `ManalensOptions`, or **Track Server Builds** in **Project Settings > Manalens** |
| Unreal | **Track Server Builds** in **Project Settings > Plugins > Manalens** |

For example, a listen server (a host who also plays) is a player. In Godot, a host started with `--headless` for a test is a server build. Turn the option on for these runs.

## Send results from the server

Results that the server decides belong to the player, not to the server. Send them from the server with the player's IDs. This is the same pattern as a web checkout: the client sends its IDs, and the backend sends the event.

1. The client sends its `anonymous_id` and `session_id` to the server, for example when it joins the match.

   | Engine | Read the IDs |
   | - | - |
   | Godot | `Manalens.get_anonymous_id()`, `Manalens.get_session_id()` |
   | Unity | `Manalens.AnonymousId`, `Manalens.SessionId` |
   | Unreal | **Get Anonymous Id**, **Get Session Id** |

2. The server sends the event with the [Node.js SDK](https://docs.manalens.dev/sdks/node.md) in server mode, or with the [HTTP API](https://docs.manalens.dev/sdks/server.md). It uses the secret key, and sets `anonymous_id` and `session_id` of that player. Add `user_id` when you know the player's account.

```ts Node.js theme={null}
import { Manalens } from '@manalens/node';

const analytics = new Manalens(process.env.MANALENS_SECRET_KEY!, {
  endpoint: 'https://events.manalens.dev',
});

// When the match ends, one event for each player.
for (const player of match.players) {
  analytics.track({
    event: 'match_end',
    anonymousId: player.anonymousId, // sent by the client when it joined
    sessionId: player.sessionId,     // sent by the client when it joined
    userId: player.accountId,        // optional
    properties: { mode: 'ranked', result: player.won ? 'win' : 'loss', duration_s: match.durationS },
  });
}
```

```bash HTTP theme={null}
curl -X POST https://events.manalens.dev/v0/events \
  -H 'Authorization: Bearer YOUR_SECRET_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"events":[{"channel":"server","type":"track","event":"match_end","anonymous_id":"3f0c5c1e-8a8e-4c1b-9d7e-2b1f4e6a9c10","session_id":"7a1e2c44-91d0-4f5e-b6a3-0c2d9e8f7a61","properties":{"mode":"ranked","result":"win"}}]}'
```

The event joins the player's profile and session, so it appears in the same reports as the client events. See [Join server events to players](https://docs.manalens.dev/sdks/server.md#join-server-events-to-players) and [Session ID of the client](https://docs.manalens.dev/sdks/server.md#session-id-of-the-client).

A server written in Godot, Unity, or Unreal has no secret key option in the game SDK. Send these events from the server process with an HTTP request (as the `curl` example), or from a backend in Node.js.

## Purchases

The client can send a `purchase` event at once. When your server checks the receipt, send the checked purchase from the server with the player's IDs. Add `validated: true` so you can tell the two apart. See [Server events](https://docs.manalens.dev/sdks/server.md#send-events).

## Checklist

* Each client uses the publishable key and tracks its own player.
* A server build sends no `game_open` and no sessions, unless you turn on the option.
* The server sends match results and checked purchases with the secret key, the player's `anonymous_id`, and the player's `session_id`.
* The secret key is never in a client build.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.