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

# Server (Node.js)

> @gamavas/pass-server: verify players, check ownership, unlock achievements and verify webhooks from your game's server.

`@gamavas/pass-server` is for your game's **server**. It has no dependencies, using only `fetch` and Web Crypto, so it runs on Node 20+, Bun, Deno and Cloudflare Workers. It ships as ESM and CommonJS, with types.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm install @gamavas/pass-server
```

> [!NOTE]
>
> The package is on its way to npm. Until it's there, call the [HTTP API](https://docs.gamavas.fun/api-reference/introduction.md) directly: it's the same three calls.

## Set up

Create a secret key on your app's **Keys** page in the [developer portal](https://developer.gamavas.fun). Keep it on your server: Gamavas refuses secret keys sent from a browser.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { GamavasPass } from "@gamavas/pass-server";

const gamavas = new GamavasPass({ secretKey: process.env.GAMAVAS_SECRET_KEY! });
```

A test key (`gmv_sk_test_…`) sees only test purchases (`gamavas.testMode` is true), and a live key only real ones.

## Verify a player

Don't trust a player ID your game sends. Have your game send its access token instead, and ask Gamavas who it belongs to:

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
const player = await gamavas.verifyPlayer(accessToken);
// { id, handle, displayName, avatarUrl, email, scopes }
```

Only live tokens issued to your game are accepted; anything else throws `unauthorized`. How to get the token in each engine is on [Your server](https://docs.gamavas.fun/pass/your-server.md).

## Ownership

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
if (await gamavas.owns(player.id)) startGame();
await gamavas.entitlement(player.id); // { owned, free, priceCents, livemode }
```

## Unlock achievements

Including those marked **server** in the portal, which a game can't unlock itself:

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
const result = await gamavas.unlock(player.id, "beat-the-boss");
// { newlyUnlocked, xpAwarded, achievement: { key, name, iconUrl } }
```

## Verify webhooks

Pass the **raw** request body, not parsed JSON:

```ts Express theme={"theme":{"light":"github-light","dark":"github-dark"}}
app.post("/gamavas", express.raw({ type: "application/json" }), async (req, res) => {
  const event = await verifyWebhook({
    body: req.body,
    signature: req.headers["gamavas-signature"],
    secret: process.env.GAMAVAS_WEBHOOK_SECRET!,
  });
  res.sendStatus(200);
});
```

```ts Hono, Workers, Bun theme={"theme":{"light":"github-light","dark":"github-dark"}}
app.post("/gamavas", async (c) => {
  const event = await verifyWebhook({
    body: await c.req.text(),
    signature: c.req.header("gamavas-signature"),
    secret: c.env.GAMAVAS_WEBHOOK_SECRET,
  });
  return c.body(null, 200);
});
```

`event` is typed by `event.type`: `order.paid`, `order.refunded`, `entitlement.revoked`, `achievement.unlocked` or `webhook.test`. `verifyWebhook` throws `invalid_signature` if the signature is wrong or more than 5 minutes old (set `toleranceSeconds` to change that).

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
switch (event.type) {
  case "order.paid":           grant(event.data.playerId); break;
  case "order.refunded":
  case "entitlement.revoked":  revoke(event.data.playerId); break;
  case "achievement.unlocked": log(event.data.key, event.data.via); break;
}
```

## Errors

Every failure throws a `GamavasPassError` with a `code`, Gamavas's `message` and, when Gamavas answered, the HTTP `status`:

| Code | When |
| - | - |
| `bad_request` | Something is missing from the request |
| `unauthorized` | A bad secret key, or a player token that's expired, revoked or for another game |
| `forbidden` | Not allowed, or the request came from a browser |
| `not_found` | Unknown achievement, or a player who hasn't connected your game |
| `rate_limited` | Too many requests: slow down |
| `server_error` | Gamavas had a problem: retry later |
| `network` | Gamavas couldn't be reached |
| `invalid_signature` | A webhook that isn't from Gamavas, or is too old |

## Options

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
new GamavasPass({
  secretKey: "gmv_sk_…",
  baseUrl: "https://staging.gamavas.fun/api/pass/v1", // only for testing against staging
  fetch: myFetch,                                      // your own fetch, if you need one
});
```


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