> ## 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.

# Link API

> Reference for POST /v0/link, the endpoint that a game calls to link the Steam account of a player with a Steam ticket.

## Link a Steam account

```http theme={null}
POST /v0/link
X-Publishable-Key: YOUR_PUBLISHABLE_KEY
Content-Type: application/json
```

```json theme={null}
{
  "provider": "steam",
  "ticket": "140000000a0b0c0d...",
  "anonymous_id": "6a2f41a2-7d36-4c8e-9a54-2b1f9b0f0c11"
}
```

The game links the Steam account that runs the game to the player's game install. The player does not sign in. Call it on `https://api.manalens.dev`; `events.manalens.dev` serves only `POST /v0/events`.

The project needs a Steam Web API key and an App ID on the [Connect Steam card](https://docs.manalens.dev/integrations/steam.md). Manalens checks the ticket with Steam (`ISteamUserAuth/AuthenticateUserTicket`) and the project's key, for the project's App ID and the identity `manalens`. The identity is fixed: a ticket made for another service is refused. Then it reads once which of your apps the account owns (`ISteamUser/GetPublisherAppOwnership`). See [Steam ownership](https://docs.manalens.dev/features/steam-ownership.md).

### Authentication

Send the project's publishable key in `X-Publishable-Key`, as for [events](https://docs.manalens.dev/api/events.md). A game server can send the secret key in `Authorization: Bearer mana_sk_...` instead. The [publishable key checks](https://docs.manalens.dev/api/authentication.md#extra-checks-on-the-publishable-key) apply: a web build must run on the project domain or an allowed origin. A game sends no origin.

### Body

| Field | Type | Rules |
| - | - | - |
| `provider` | string | `steam`. Other stores come later. |
| `ticket` | string | The bytes of the Steam ticket from `GetAuthTicketForWebApi`, as a hex string: 32 to 4096 hex characters, an even number. |
| `anonymous_id` | string | The SDK's anonymous ID of this game install (a UUID). |
| `user_id` | string | Optional, 1 to 255 characters. Read only with the secret key (a game server). With the publishable key it is checked but not stored, because a client can send any user ID. A stored user ID is never replaced. |

Other fields are ignored. The ticket must come from `GetAuthTicketForWebApi("manalens")`.

A verified link binds the Steam account to the game install that made the ticket. It does not make the player identified: the player's user ID comes from `identify`, as before.

### Responses

| Status | Body | When |
| - | - | - |
| `200` | `{ "linked": true, "provider": "steam" }` | Steam accepted the ticket. The response never contains the SteamID. |
| `400` | `INVALID_JSON` | The body is not valid JSON. |
| `400` | `INVALID_BODY` | A field is missing or does not match the rules. The message names the field. |
| `400` | `STEAM_TICKET_INVALID` | Steam did not accept the ticket: it is expired, for another app, or not made with the identity `manalens`. Get a new ticket and try again. |
| `401` | `UNAUTHORIZED` | The key is missing or wrong, or the request has both keys. |
| `403` | `FORBIDDEN` or `WORKSPACE_SUSPENDED` | The origin does not match the project, or the workspace takes no events. |
| `409` | `STEAM_NOT_CONNECTED` | The project has no Steam Web API key or App ID. |
| `413` | `TOO_LARGE` | The body is larger than 2 MB. |
| `429` | `RATE_LIMITED` | More than 30 link calls from one IP address in 10 minutes, more than 5 from one game install in 10 minutes, or more than 600 for the project in one minute. Read `Retry-After`. A request that fails an earlier check (body, key, Steam not connected, a refused Steam key) does not count. |
| `502` | `STEAM_KEY_REFUSED` | Steam refused the project's Steam Web API key. Manalens does not try a refused key again until an owner saves a key on the Connect Steam card. |
| `502` | `STEAM_UNAVAILABLE` | Manalens could not reach Steam. Try again later. |
| `500` | `INTERNAL` | An unexpected error. The body has a `request_id`. |
| `503` | `NOT_CONFIGURED` | The server cannot read the saved key, or integration encryption is not configured on the server. |

Errors use the [error format](https://docs.manalens.dev/api/errors.md): `{ "error": { "code": "...", "message": "..." } }`.

When Steam accepts the ticket but the ownership read fails, the link is saved and the call still returns `200`. Ownership is read at the next link.

## SDK call

Each SDK has the call. The SDK sends the ticket as hex with the anonymous ID, after consent. It does not send a refused ticket again, and tries once more only after a `429` with `Retry-After`.

| SDK | Call | Ticket |
| - | - | - |
| [Godot](https://docs.manalens.dev/sdks/godot.md#verify-steam-ownership) | `link_steam()`, or the option `steam_link: true` | With GodotSteam, the SDK calls `Steam.getAuthTicketForWebApi("manalens")` and waits for the `get_ticket_for_web_api` signal. Or pass your own ticket bytes. |
| [Unity](https://docs.manalens.dev/sdks/unity.md#verify-steam-ownership) | `Manalens.LinkSteam()`, or the option `SteamLink = true` | With Steamworks.NET, the SDK calls `SteamUser.GetAuthTicketForWebApi("manalens")` and waits for `GetTicketForWebApiResponse_t`. Or pass the bytes to `LinkSteam(ticket)` (Facepunch: `GetAuthTicketForWebApiAsync("manalens")`). |
| [Unreal](https://docs.manalens.dev/sdks/unreal.md#verify-steam-ownership) | `LinkSteamTicket(Ticket)` | The game gets a Web API ticket with the identity `manalens` from the Steamworks SDK or the Steam Online Subsystem and passes the bytes. |
| [Web](https://docs.manalens.dev/sdks/web.md#verify-steam-ownership) | `link({ provider: 'steam', ticket })` | The ticket as hex. |
| [Node](https://docs.manalens.dev/sdks/node.md#verify-steam-ownership) | `link({ provider: 'steam', ticket, anonymousId, userId })` | A game server with the secret key; `userId` is optional. |

Call it once after Steam starts, for example on the main menu. A new ticket for each call is fine. Do not send a SteamID that the game reads by itself: only a ticket proves the account.

Without an SDK, send the request yourself:

```bash theme={null}
curl https://api.manalens.dev/v0/link \
  -H "X-Publishable-Key: $MANALENS_PUBLISHABLE_KEY" \
  -H "Content-Type: application/json" \
  -d '{"provider":"steam","ticket":"<hex>","anonymous_id":"<anonymous id>"}'
```


## OpenAPI

````yaml POST /v0/link
openapi: 3.1.0
info:
  title: Manalens API
  version: 0.1.0
  description: >-
    The public API of Manalens: event ingestion, Steam account links, player
    traits and the Query API. Success bodies are the resource, with no envelope.
    Errors use the `Error` format. See https://docs.manalens.dev/api/overview.
servers:
  - url: https://api.manalens.dev
    description: API
  - url: https://events.manalens.dev
    description: Event ingestion
security: []
tags:
  - name: Events
    description: Send website, game and server events
  - name: Accounts
    description: Link the Steam account of a player to the game install
  - name: Players
    description: Set and delete the traits of identified players
  - name: Query
    description: Read analytics reports with an API key
paths:
  /v0/link:
    post:
      tags:
        - Accounts
      summary: Link a Steam account
      description: >-
        Link the Steam account that runs the game to the game install
        (`anonymous_id`) with a Steam ticket. The player does not sign in. Use
        the publishable key in `X-Publishable-Key`, or the secret key as
        `Authorization: Bearer` from a game server. With the publishable key, a
        web build must run on the project domain or an allowed origin. A game
        sends no Origin.


        Manalens checks the ticket with Steam
        (`ISteamUserAuth/AuthenticateUserTicket`), with the project's Steam Web
        API key, the project's App ID and the fixed identity `manalens`. Then it
        reads which apps of the project the account owns
        (`ISteamUser/GetPublisherAppOwnership`): at the first link, and again
        when the last read is older than 15 days. When the ownership read fails,
        the link is still saved and the answer is 200. The response never
        contains the SteamID.


        The checks run in this order: the body, the key, the Steam connection,
        the rate limits, then Steam. A request that fails a check before the
        rate limits does not count against them. Send it to the API host only:
        events.manalens.dev serves only `POST /v0/events`.
      operationId: linkAccount
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                provider:
                  type: string
                  const: steam
                  description: The store. Only `steam` today.
                ticket:
                  type: string
                  pattern: ^(?:[0-9a-fA-F]{2}){16,2048}$
                  description: >-
                    The bytes of the Steam ticket from
                    `GetAuthTicketForWebApi("manalens")`, as a hex string: 32 to
                    4096 hex characters, an even number
                anonymous_id:
                  type: string
                  pattern: >-
                    ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$
                  description: The SDK anonymous ID of this game install (a UUID)
                user_id:
                  description: >-
                    Secret key only (a game server): the user ID to store with
                    the link. With the publishable key it is checked but not
                    stored. A stored user ID is never replaced.
                  type: string
                  minLength: 1
                  maxLength: 255
                  pattern: ^[^\u0000-\u001f]+$
              required:
                - provider
                - ticket
                - anonymous_id
              description: Fields that are not in this list are ignored.
            example:
              provider: steam
              ticket: 140000000a0b0c0d0e0f101112131415
              anonymous_id: 6a2f41a2-7d36-4c8e-9a54-2b1f9b0f0c11
      responses:
        '200':
          description: Steam accepted the ticket and the link is saved
          content:
            application/json:
              schema:
                type: object
                properties:
                  linked:
                    type: boolean
                    const: true
                  provider:
                    type: string
                    const: steam
                required:
                  - linked
                  - provider
              example:
                linked: true
                provider: steam
        '400':
          description: >-
            `INVALID_JSON`: the body is not valid JSON. `INVALID_BODY`: a field
            is missing or does not match the rules. The message names the field.
            `STEAM_TICKET_INVALID`: Steam did not accept the ticket (it is
            expired, for another app, or not made with the identity `manalens`).
            Get a new ticket and try again.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error:
                  code: STEAM_TICKET_INVALID
                  message: >-
                    Steam did not accept this ticket. Get a new ticket for the
                    identity "manalens" and try again.
        '401':
          description: >-
            `UNAUTHORIZED`: the key is missing, malformed, unknown or revoked,
            or in the wrong header, or the request has both keys
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '403':
          description: >-
            `FORBIDDEN`: the Origin does not match the project (publishable key
            only). `WORKSPACE_SUSPENDED`: the workspace takes no events.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '409':
          description: >-
            `STEAM_NOT_CONNECTED`: the project has no Steam Web API key or App
            ID on the Connect Steam card
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '413':
          description: '`TOO_LARGE`: the body is larger than 2 MB'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '429':
          description: >-
            `RATE_LIMITED`: more than 30 link calls from one IP address in 10
            minutes, more than 5 from one game install in 10 minutes, or more
            than 600 for the project in one minute. The headers give the limit
            that the request hit.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
          headers:
            Retry-After:
              description: Seconds to wait before a retry
              schema:
                type: integer
            RateLimit-Limit:
              description: The limit of the window, when it is known
              schema:
                type: integer
            RateLimit-Policy:
              description: '`<limit>;w=<window seconds>`, when the limit is known'
              schema:
                type: string
            RateLimit-Remaining:
              description: Always 0 on a 429
              schema:
                type: integer
            RateLimit-Reset:
              description: Seconds until the window resets (the same as Retry-After)
              schema:
                type: integer
        '500':
          description: '`INTERNAL`: an unexpected error. The body has a `request_id`.'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '502':
          description: >-
            `STEAM_KEY_REFUSED`: Steam refused the project's Steam Web API key.
            Manalens does not use a refused key again until an owner saves a
            key. `STEAM_UNAVAILABLE`: Manalens could not reach Steam. Retry
            later.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '503':
          description: >-
            `NOT_CONFIGURED`: the server cannot read the saved Steam key, or
            integration encryption is not configured on the server
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - PublishableKey: []
        - SecretKey: []
      servers:
        - url: https://api.manalens.dev
          description: API
components:
  schemas:
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: string
              description: A stable, machine-readable code. Branch on it.
            message:
              type: string
              description: A human-readable text. The wording can change.
            request_id:
              description: 'INTERNAL only: quote it when you contact support'
              type: string
            issues:
              description: 'INVALID_EVENT only: up to five validation issues'
              type: array
              items:
                type: object
                properties:
                  path:
                    type: string
                    description: Where the problem is, such as `events.0.properties`
                  message:
                    type: string
                    description: What is wrong, such as `at most 50 keys`
                required:
                  - path
                  - message
            refused:
              description: >-
                INVALID_EVENT when no event of the batch is accepted: each
                refused event (the same list as `errors`, kept for SDKs that
                read it here)
              type: array
              items:
                type: object
                properties:
                  index:
                    type: integer
                    minimum: -9007199254740991
                    maximum: 9007199254740991
                    description: The position of the event in the batch
                  message_id:
                    description: The message_id of the event, or null when it has none
                    type:
                      - string
                      - 'null'
                  code:
                    type: string
                    description: >-
                      The reason code, such as `INVALID_EVENT`,
                      `SESSION_ID_REQUIRED` or `FORBIDDEN`
                  message:
                    type: string
                    description: 'What is wrong, such as `properties: at most 50 keys`'
                required:
                  - index
                  - message_id
                  - code
                  - message
            results:
              description: 'INVALID_TRAITS of a batch trait update: the result of each item'
              type: array
              items:
                type: object
                properties:
                  index:
                    type: integer
                    minimum: -9007199254740991
                    maximum: 9007199254740991
                  user_id:
                    type: string
                  error:
                    type: object
                    properties:
                      code:
                        type: string
                      message:
                        type: string
                    required:
                      - code
                      - message
                required:
                  - index
            feature:
              description: >-
                PLAN_UPGRADE_REQUIRED only: the plan feature that the request
                needs
              type: string
            billing_url:
              description: 'PLAN_UPGRADE_REQUIRED only: the billing page of the workspace'
              type: string
          required:
            - code
            - message
      required:
        - error
      description: >-
        The error format of every endpoint. See
        https://docs.manalens.dev/api/errors.
  securitySchemes:
    PublishableKey:
      type: apiKey
      in: header
      name: X-Publishable-Key
      description: >-
        The publishable key (`mana_pk_...`). It sends web and app events and
        links Steam accounts.
    SecretKey:
      type: http
      scheme: bearer
      description: >-
        The secret key (`mana_sk_...`), from your backend only. It sends server
        events and links Steam accounts.

````

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