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

# Create API key

> Mint a new API key. The plaintext token is returned exactly once in this
response — store it securely. Subsequent reads expose only the prefix and
last four characters.




## OpenAPI

````yaml https://api.nexspace365.com/api/openapi.json post /api/api-keys
openapi: 3.0.3
info:
  title: NexSpace 365 API
  description: >
    Multi-industry enterprise workforce management platform — scheduling, HR,

    payroll, CRM, AI, and messaging suites.


    ## Authentication

    Three schemes: session cookie, JWT bearer, or API key (`nex_live_…` /

    `nex_test_…` / `nex_pat_…`). AI agents should use API keys with appropriate

    scopes.


    ## Authorization

    Endpoints require specific permissions via the RBAC system:

    - **Internal team**: Full platform access

    - **Facility users**: Scoped to their org/facilities

    - **Staff**: Limited to own data


    ## Error Contract

    All errors return `{ error: { message, code?, suggestion?, retryable? },
    requestId? }`.

    The `suggestion` field hints at how to fix the request; `retryable` signals
    whether

    retry-with-backoff is appropriate.


    ## Versioning

    Pin your integration to a specific API version via the `NexSpace-Version`
    header

    (date-based, e.g. `2026-05-10`). When absent, the latest version is assumed.

    Deprecated versions include `Sunset` and `Deprecation` headers. Discovery at

    `GET /.well-known/api-versions`.


    ## Idempotency

    Write operations (POST/PUT/PATCH) accept an `Idempotency-Key` header. If

    the same key + body combination is sent again within 24 hours, the original

    response is replayed. Different body with the same key returns 409 Conflict.


    ## MCP (Model Context Protocol)

    AI agents interact via `POST /mcp` using JSON-RPC 2.0. Call `tools/list` to

    discover available verbs, then `tools/call` to invoke them.
  version: 1.0.0
  contact:
    name: NexSpace API Support
    email: support@nexspace365.com
  license:
    name: MIT
    url: https://opensource.org/licenses/MIT
servers:
  - url: https://api.nexspace365.com
    description: Production server
security:
  - bearerAuth: []
  - apiKeyAuth: []
tags:
  - name: Authentication
    description: User authentication and session management
  - name: Facilities
    description: Facility management
  - name: Staff
    description: Staff member management and profiles
  - name: Credentials
    description: Staff credential tracking — licenses, certifications, and training
  - name: Shifts
    description: Shift scheduling and management
  - name: Dashboard
    description: Dashboard statistics and widgets
  - name: API Keys
    description: API key lifecycle — create, list, revoke, rotate
  - name: MCP
    description: Model Context Protocol endpoint for AI agent tool calling
  - name: OAuth
    description: OAuth 2.1 authorization server — DCR, authorize, token
  - name: Webhooks
    description: Outbound webhook subscription management
  - name: Analytics
    description: API usage analytics and observability
paths:
  /api/api-keys:
    post:
      tags:
        - API Keys
      summary: Create API key
      description: >
        Mint a new API key. The plaintext token is returned exactly once in this

        response — store it securely. Subsequent reads expose only the prefix
        and

        last four characters.
      operationId: createApiKey
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateApiKeyRequest'
      responses:
        '201':
          description: API key created (plaintext token in `key` field — display once)
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ApiKey'
                  - type: object
                    properties:
                      key:
                        type: string
                        description: Plaintext token — store securely, never shown again
        '400':
          description: Validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - bearerAuth: []
components:
  schemas:
    CreateApiKeyRequest:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 120
          description: Human-readable key name
        scopes:
          type: array
          items:
            type: string
            pattern: ^[a-z_]+:[a-z*]+$
          minItems: 1
          description: Permission scopes (e.g. ['shifts:read', 'staff:*'])
        expiresAt:
          type: string
          format: date-time
          description: Optional expiry timestamp
        ipAllowlist:
          type: array
          items:
            type: string
          description: Optional IPv4/IPv6 CIDR allowlist
        rateLimitPerMin:
          type: integer
          minimum: 1
          maximum: 6000
          default: 60
        asPat:
          type: boolean
          default: false
          description: Mint as personal access token (nex_pat_ prefix)
      required:
        - name
        - scopes
      example:
        name: CI deploy key
        scopes:
          - shifts:read
          - facilities:read
        expiresAt: '2026-08-09T00:00:00Z'
        rateLimitPerMin: 120
    ApiKey:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        prefix:
          type: string
          description: Token prefix (e.g. nex_live_, nex_test_, nex_pat_)
        lastFour:
          type: string
          description: Last 4 characters of the token
        scopes:
          type: array
          items:
            type: string
        facilityId:
          type: integer
          nullable: true
        ipAllowlist:
          type: array
          items:
            type: string
        rateLimitPerMin:
          type: integer
        lastUsedAt:
          type: string
          format: date-time
          nullable: true
        expiresAt:
          type: string
          format: date-time
          nullable: true
        revokedAt:
          type: string
          format: date-time
          nullable: true
        rotationGraceUntil:
          type: string
          format: date-time
          nullable: true
        createdAt:
          type: string
          format: date-time
      example:
        id: 15
        name: CI deploy key
        prefix: nex_live_
        lastFour: 8f2a
        scopes:
          - shifts:read
          - facilities:read
        facilityId: null
        ipAllowlist: []
        rateLimitPerMin: 120
        lastUsedAt: '2026-05-11T07:59:00Z'
        expiresAt: '2026-08-09T00:00:00Z'
        revokedAt: null
        rotationGraceUntil: null
        createdAt: '2026-05-11T07:00:00Z'
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              description: Human-readable error message
            code:
              type: string
              description: Machine-readable error code (e.g. VALIDATION_FAILED, NOT_FOUND)
            details:
              type: object
              description: Structured details (e.g. Zod validation issues)
            suggestion:
              type: string
              description: Hint for agents on how to recover
            retryable:
              type: boolean
              description: Whether the same request might succeed on retry
          required:
            - message
        requestId:
          type: string
          description: Correlation ID for logs and Sentry
      required:
        - error
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: JWT token authentication
    apiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: NexSpaceApiKey
      description: |
        API key or personal access token (NEX-1042). Send as
        `Authorization: Bearer <token>`. Token format:
          - `nex_live_…`  — live API key (server-to-server)
          - `nex_test_…`  — sandbox API key
          - `nex_pat_…`   — personal access token (acts as the issuing user)

        Authorization is decided by the key's `scopes` array, not the
        owning user's RBAC role. See `POST /api/api-keys` to mint a key.

````