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

# Create API Key

> Create an API key for yourself. The response carries the key string in `key`: it is shown **only once**, so store it safely right away; later calls return the key's details but never the key again. Send it as `Authorization: Bearer <key>`.

- `exp`: when the key expires, at most 365 days ahead; omitted, the key lasts 365 days.
- `narrowing`: optional cap per resource (`{resource: level}`, levels `none`, `read`, `use`, `manage`); the key then acts with the lower of the cap and your own level. Omitted, the key acts with your permissions.
- `auth_scopes`: legacy scope list, kept for older clients; prefer `narrowing`.

Create keys while signed in (for example from the Copilot): a request authenticated with an API key cannot create another key.

**Failure Modes:**
- **400**: `Invalid scopes: ...` when `auth_scopes` asks for a scope you do not hold
- **401**: Token is missing, invalid, expired or revoked
- **403**: `access_denied` when your role cannot manage API keys, or the request is authenticated with an API key
- **422**: `config_invalid` when `exp` is in the past or more than 365 days ahead, or `narrowing` names an unknown resource or level or goes above your own level; or an invalid body

**Required Auth Scopes:**
- `api_keys:write`



## OpenAPI

````yaml /api-reference/openapi.json post /auth/api-keys
openapi: 3.1.0
info:
  title: SWITCH API
  version: '1.0'
  description: >-
    The SWITCH API gives mobility and logistics teams programmatic access to
    SWITCH forecasts, fleet operations, routing and analytics. Every request is
    authenticated with an API key sent as a bearer token.
servers:
  - url: https://stage-api.getswitch.io
    description: Stage
security: []
tags:
  - name: Forecast
    description: Demand forecasts per zone and time window.
  - name: Operations
    description: Fleet imbalance, rebalancing suggestions and generated tasks.
  - name: Routing
    description: Route optimisation for single and multiple fleets.
  - name: Routing Sessions
    description: Saved routing sessions, their routes and drivers.
  - name: Routing Operations
    description: Complete, undo and re-route the stops of a route.
  - name: Fleet Management
    description: Your depots, stations, drivers and cargo vehicles.
  - name: Integrations
    description: Vehicles, statuses and commands on your fleet platform.
  - name: Station Based Analytics
    description: Analytics for station-based (docked) services.
  - name: Zone Based Analytics
    description: Analytics for free-floating services, by zone.
  - name: Free Float Analytics
    description: Trip history for free-floating services.
  - name: Transit Analytics
    description: Punctuality and service delivery for public transit.
  - name: Live
    description: Real-time fleet, delivery and transit status.
  - name: Grids
    description: Spatial grids used by forecasts and analytics.
  - name: API Keys
    description: Create and manage the API keys you call this API with.
paths:
  /auth/api-keys:
    post:
      tags:
        - API Keys
      summary: Create API Key
      description: >-
        Create an API key for yourself. The response carries the key string in
        `key`: it is shown **only once**, so store it safely right away; later
        calls return the key's details but never the key again. Send it as
        `Authorization: Bearer <key>`.


        - `exp`: when the key expires, at most 365 days ahead; omitted, the key
        lasts 365 days.

        - `narrowing`: optional cap per resource (`{resource: level}`, levels
        `none`, `read`, `use`, `manage`); the key then acts with the lower of
        the cap and your own level. Omitted, the key acts with your permissions.

        - `auth_scopes`: legacy scope list, kept for older clients; prefer
        `narrowing`.


        Create keys while signed in (for example from the Copilot): a request
        authenticated with an API key cannot create another key.


        **Failure Modes:**

        - **400**: `Invalid scopes: ...` when `auth_scopes` asks for a scope you
        do not hold

        - **401**: Token is missing, invalid, expired or revoked

        - **403**: `access_denied` when your role cannot manage API keys, or the
        request is authenticated with an API key

        - **422**: `config_invalid` when `exp` is in the past or more than 365
        days ahead, or `narrowing` names an unknown resource or level or goes
        above your own level; or an invalid body


        **Required Auth Scopes:**

        - `api_keys:write`
      operationId: create_api_key
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ApiKeyCreate'
      responses:
        '201':
          description: API key successfully created with metadata and key string.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKeyFirstTimePublic'
        '400':
          description: >-
            Bad Request. Invalid authorization scopes specified - user lacks
            permission for requested scopes.
        '401':
          description: >-
            Unauthorized. Authentication credentials missing, invalid, expired,
            or revoked.
        '403':
          description: Forbidden. Missing required `api_keys:write` authorization scope.
        '422':
          description: >-
            Unprocessable Entity. Invalid request body, or `config_invalid`:
            `exp` in the past or more than 365 days ahead, `narrowing` above
            your own level.
        '500':
          description: >-
            Internal Server Error. Database transaction failure or token
            generation error.
      security:
        - HTTPBearer: []
        - HTTPBearer: []
components:
  schemas:
    ApiKeyCreate:
      properties:
        name:
          type: string
          title: Name
          description: Name of the new API key.
        exp:
          type:
            - string
            - 'null'
          format: date-time
          title: Exp
          description: >-
            When the key expires (ISO 8601), in the future and at most 365 days
            ahead (422 `config_invalid` otherwise). Omitted: 365 days from now.
        auth_scopes:
          items:
            type: string
          type:
            - array
            - 'null'
          title: Auth Scopes
          description: >-
            Deprecated, prefer `narrowing`: legacy scope slugs for the key, each
            one you hold (400 otherwise). Without `narrowing`, the key's cap is
            derived from them. Omitted: all your scopes.
        narrowing:
          additionalProperties:
            type: string
            enum:
              - none
              - read
              - use
              - manage
          type:
            - object
            - 'null'
          title: Narrowing
          description: >-
            Optional cap per resource, `{resource: level}` with levels `none`,
            `read`, `use`, `manage` (resources as in `access.resources` of `GET
            /auth/me`). The key acts with the lower of the cap and your own
            level; a cap above your level is refused (422). Omitted: the key
            acts with your permissions.
      type: object
      required:
        - name
      title: ApiKeyCreate
      description: >-
        Schema for creating a new API key.


        Used in API requests to create a new API key resource, optionally
        specifying

        expiration and scopes.
      examples:
        - exp: '2027-11-30T09:00:00Z'
          name: Nightly forecast export
          narrowing:
            fleet.vehicles: read
            forecast: read
    ApiKeyFirstTimePublic:
      properties:
        uuid:
          type: string
          title: Uuid
          description: Unique UUID identifier for the API key.
        name:
          type: string
          title: Name
          description: Name of the API key.
        exp:
          type:
            - string
            - 'null'
          format: date-time
          title: Exp
          description: When the key expires (UTC).
        created_at:
          type: string
          format: date-time
          title: Created At
          description: Datetime when the API key was created.
        auth_scopes:
          items:
            $ref: '#/components/schemas/AuthScopePublic'
          type: array
          title: Auth Scopes
          description: List of authorization scopes assigned to the API key.
        key:
          type: string
          title: Key
          description: >-
            The API key itself, returned only in this response: store it now.
            Send it as `Authorization: Bearer <key>`.
      type: object
      required:
        - uuid
        - name
        - created_at
        - auth_scopes
        - key
      title: ApiKeyFirstTimePublic
      description: >-
        Public schema for an API key returned at creation time.


        Extends ApiKeyPublic to include the actual key string, which is only
        shown once at

        creation.
      examples:
        - auth_scopes:
            - description: Read forecast data
              slug: forecast:read
            - description: Read access to upstream vehicles
              slug: integrations:vehicles:read
          created_at: '2026-11-30T09:00:00Z'
          exp: '2027-11-30T09:00:00Z'
          key: >-
            eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFWEFNUExFIjoibm90LWEtcmVhbC1rZXkifQ.EXAMPLE_SIGNATURE_NOT_A_REAL_KEY
          name: Nightly forecast export
          uuid: 3f2b8c1e-5d4a-4e7b-9c0d-1a2b3c4d5e6f
    AuthScopePublic:
      properties:
        slug:
          type: string
          title: Slug
          description: Unique slug identifier for the authorization scope.
        description:
          type: string
          title: Description
          description: Description of the authorization scope.
      type: object
      required:
        - slug
        - description
      title: AuthScopePublic
      description: >-
        Public schema for an authorization scope.


        Exposes information about an available authorization scope to API
        consumers.
  securitySchemes:
    HTTPBearer:
      type: http
      scheme: bearer
      description: >-
        An API key, sent as `Authorization: Bearer <key>`. Create one in the
        SWITCH Copilot under **Settings → API keys** (needs the API keys
        permission in your company), or with `POST /auth/api-keys`. The key is
        shown once, at creation.

````

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