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

# Create an API key

> Creates a test or live key on one of your trading accounts with the scopes and limits you choose, up to the account’s own. The secret is returned once in this response; store it, because it cannot be shown again.



## OpenAPI

````yaml api-reference/openapi.json POST /api-keys
openapi: 3.0.0
info:
  title: Lean API
  version: 0.1.0
  description: >-
    Programmatic access to Lean: canonical sports markets, quotes, orders,
    positions and balances.
servers:
  - url: '{baseUrl}/v1'
    description: Lean API
    variables:
      baseUrl:
        default: https://api.lean.xyz
        description: >-
          The Lean API base URL, without /v1. Same value as LEAN_API_URL in the
          guides.
security:
  - apiKey: []
paths:
  /api-keys:
    servers:
      - url: '{baseUrl}'
        description: Lean API
        variables:
          baseUrl:
            default: https://api.lean.xyz
            description: The Lean API base URL. Same value as LEAN_API_URL in the guides.
    post:
      tags:
        - API keys
      summary: Create an API key
      description: >-
        Creates a test or live key on one of your trading accounts with the
        scopes and limits you choose, up to the account’s own. The secret is
        returned once in this response; store it, because it cannot be shown
        again.
      operationId: createApiKey
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  minLength: 1
                  maxLength: 80
                tradingAccountId:
                  type: string
                  format: uuid
                scopes:
                  type: array
                  items:
                    type: string
                    enum:
                      - read
                      - trade
                      - route
                      - withdraw
                  minItems: 1
                mode:
                  type: string
                  enum:
                    - test
                    - live
                  default: test
                maxOrderNotionalMicros:
                  type: integer
                  minimum: 0
                  exclusiveMinimum: true
                  maximum: 9007199254740991
                dailyNotionalLimitMicros:
                  type: integer
                  minimum: 0
                  exclusiveMinimum: true
                  maximum: 9007199254740991
                expiresInDays:
                  type: integer
                  minimum: 1
                  maximum: 90
                maxWithdrawalMicros:
                  type: integer
                  minimum: 0
                  exclusiveMinimum: true
                  maximum: 9007199254740991
                dailyWithdrawalLimitMicros:
                  type: integer
                  minimum: 0
                  exclusiveMinimum: true
                  maximum: 9007199254740991
              required:
                - name
                - tradingAccountId
                - scopes
                - maxOrderNotionalMicros
                - dailyNotionalLimitMicros
                - expiresInDays
            example:
              name: Pricing bot
              tradingAccountId: 3f6c2a8e-91d4-4b7a-8c25-0e4d7b19a6f3
              scopes:
                - read
                - trade
              mode: test
              maxOrderNotionalMicros: 100000000
              dailyNotionalLimitMicros: 500000000
              expiresInDays: 90
      responses:
        '200':
          description: The new key and its secret, shown once
          content:
            application/json:
              schema:
                oneOf:
                  - type: object
                    properties:
                      success:
                        type: boolean
                        enum:
                          - true
                      data:
                        type: object
                        properties:
                          apiKey:
                            type: object
                            properties:
                              id:
                                type: string
                                format: uuid
                              name:
                                type: string
                                minLength: 1
                              keyPrefix:
                                type: string
                                minLength: 1
                              tradingAccountId:
                                type: string
                                format: uuid
                              scopes:
                                type: array
                                items:
                                  type: string
                                  enum:
                                    - read
                                    - trade
                                    - route
                                    - withdraw
                                minItems: 1
                              mode:
                                type: string
                                enum:
                                  - test
                                  - live
                              maxOrderNotionalMicros:
                                type: integer
                                minimum: 0
                                exclusiveMinimum: true
                              dailyNotionalLimitMicros:
                                type: integer
                                minimum: 0
                                exclusiveMinimum: true
                              maxWithdrawalMicros:
                                type: integer
                                nullable: true
                                minimum: 0
                                exclusiveMinimum: true
                              dailyWithdrawalLimitMicros:
                                type: integer
                                nullable: true
                                minimum: 0
                                exclusiveMinimum: true
                              expiresAt:
                                type: string
                                nullable: true
                                format: date-time
                              revokedAt:
                                type: string
                                nullable: true
                                format: date-time
                              lastUsedAt:
                                type: string
                                nullable: true
                                format: date-time
                              createdAt:
                                type: string
                                format: date-time
                            required:
                              - id
                              - name
                              - keyPrefix
                              - tradingAccountId
                              - scopes
                              - mode
                              - maxOrderNotionalMicros
                              - dailyNotionalLimitMicros
                              - maxWithdrawalMicros
                              - dailyWithdrawalLimitMicros
                              - expiresAt
                              - revokedAt
                              - lastUsedAt
                              - createdAt
                          secret:
                            type: string
                            minLength: 1
                        required:
                          - apiKey
                          - secret
                      meta:
                        type: object
                        properties:
                          timestampMs:
                            type: integer
                            minimum: 0
                          requestId:
                            type: string
                            minLength: 1
                          traceId:
                            type: string
                            minLength: 1
                          cachedAtMs:
                            type: integer
                            minimum: 0
                          source:
                            type: string
                            enum:
                              - cache
                              - database
                              - clickhouse
                              - upstream
                              - fallback
                              - internal
                        required:
                          - timestampMs
                          - requestId
                    required:
                      - success
                      - data
                      - meta
                  - $ref: '#/components/schemas/ApiError'
              examples:
                Created:
                  summary: The secret is shown once
                  value:
                    success: true
                    data:
                      apiKey:
                        id: 0c4f8e2a-9b1d-4e73-a6f5-3d8b2c7e1a09
                        name: Pricing bot
                        mode: test
                        keyPrefix: lean_sk_test_xxxxxxxx
                        tradingAccountId: 3f6c2a8e-91d4-4b7a-8c25-0e4d7b19a6f3
                        scopes:
                          - read
                          - trade
                          - read
                          - trade
                        maxOrderNotionalMicros: 100000000
                        dailyNotionalLimitMicros: 500000000
                        maxWithdrawalMicros: null
                        dailyWithdrawalLimitMicros: null
                        expiresAt: '2027-01-27T18:00:00.000Z'
                        revokedAt: null
                        lastUsedAt: null
                        createdAt: '2026-10-29T18:00:00.000Z'
                      secret: >-
                        lean_sk_test_xxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
        '400':
          description: API error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                Invalid limits:
                  summary: The daily limit is below the per order limit
                  value:
                    success: false
                    error:
                      code: VALIDATION_ERROR
                      message: Invalid request
                      details:
                        - path: dailyNotionalLimitMicros
                          message: >-
                            dailyNotionalLimitMicros must be at least
                            maxOrderNotionalMicros
                        - path: dailyNotionalLimitMicros
                          message: >-
                            dailyNotionalLimitMicros must be at least
                            maxOrderNotionalMicros
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
        '401':
          description: API error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                Missing token:
                  summary: No Authorization header
                  value:
                    success: false
                    error:
                      code: UNAUTHORIZED
                      message: Missing authorization header
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
                Invalid token:
                  summary: Expired, malformed, or an API key
                  value:
                    success: false
                    error:
                      code: UNAUTHORIZED
                      message: Invalid or expired token
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
        '403':
          description: API error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                No access:
                  summary: The account does not have access to Lean yet
                  value:
                    success: false
                    error:
                      code: FORBIDDEN
                      message: >-
                        You do not have a whitelist code. Please join the
                        waitlist and fill out the form on the landing page.
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
        '404':
          description: API error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                No such account:
                  summary: tradingAccountId is not one of yours
                  value:
                    success: false
                    error:
                      code: NOT_FOUND
                      message: Trading account not found
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
        '409':
          description: Too many active keys
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                Too many keys:
                  summary: Revoke a key first
                  value:
                    success: false
                    error:
                      code: CONFLICT
                      message: At most 10 active API keys; revoke one first
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
        '422':
          description: A limit is above the trading account's own
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              example:
                success: false
                error:
                  code: VALIDATION_ERROR
                  message: A limit is above the trading account's own
                meta:
                  timestampMs: 1792771200000
                  requestId: 7d2c9f4e-1b3a-4e8d-9c6f-2a5b8e0d4f17
        '500':
          description: API error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiError'
              examples:
                Internal error:
                  summary: Unexpected server error
                  value:
                    success: false
                    error:
                      code: INTERNAL_ERROR
                      message: An internal error occurred.
                    meta:
                      timestampMs: 1791741600000
                      requestId: b8c2f5e1-6a3d-4097-8e4b-2f1d7a9c0e63
      security:
        - sessionToken: []
components:
  schemas:
    ApiError:
      type: object
      properties:
        success:
          type: boolean
          enum:
            - false
        error:
          type: object
          properties:
            code:
              type: string
              enum:
                - VALIDATION_ERROR
                - NOT_FOUND
                - UNAUTHORIZED
                - FORBIDDEN
                - CONFLICT
                - UPSTREAM_ERROR
                - RATE_LIMITED
                - INTERNAL_ERROR
                - FEATURE_DISABLED
                - CONDITIONAL_SELL_REJECTED
                - CONDITIONAL_SELL_ACTIVE
            message:
              type: string
              minLength: 1
            details:
              nullable: true
          required:
            - code
            - message
        meta:
          type: object
          properties:
            timestampMs:
              type: integer
              minimum: 0
            requestId:
              type: string
              minLength: 1
            traceId:
              type: string
              minLength: 1
            cachedAtMs:
              type: integer
              minimum: 0
            source:
              type: string
              enum:
                - cache
                - database
                - clickhouse
                - upstream
                - fallback
                - internal
          required:
            - timestampMs
            - requestId
      required:
        - success
        - error
        - meta
  securitySchemes:
    apiKey:
      type: http
      scheme: bearer
      description: >-
        A Lean API key, sent as `Authorization: Bearer <key>`. A test key
        (`lean_sk_test_...`) simulates orders and fills with no money; a live
        key (`lean_sk_live_...`) trades for real within the key's limits. Every
        successful response says which mode answered in `data.livemode`.
    sessionToken:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: >-
        The session token of a Lean user who is signed in, sent as
        `Authorization: Bearer <token>`. Only the API key endpoints accept it,
        and an API key cannot create or revoke keys.

````

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