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

# Update Api Key

> Update an API key's name or scopes.

This endpoint allows partial updates - only the fields provided in the request
will be modified. The key_hash is never exposed in the response.

Args:
    key_id: API key ID to update
    request: Partial update payload with optional name and/or scopes
    db: Async database session
    tenant_id: Active tenant ID
    current_user: Authenticated user (must be tenant_admin or tenant_owner)

Returns:
    APIKeyResponse: Updated API key metadata (without key_hash)

Raises:
    HTTPException 404: If API key not found or doesn't belong to tenant
    HTTPException 403: If user doesn't have permission



## OpenAPI

````yaml /openapi.json patch /auth/api-keys/{key_id}
openapi: 3.1.0
info:
  title: Factio SRI API
  description: >-
    Plataforma headless, multi-tenant, API-first para emitir comprobantes
    electrónicos válidos ante el SRI de Ecuador.


    ## Autenticación


    Soporta tres métodos:

    - **JWT Bearer por Google OAuth**: `/auth/google/callback`

    - **JWT Bearer por email/password**: `/auth/login`

    - **API Key**: header `X-API-Key` con clave generada en `/auth/api-keys`


    ## Multi-tenant


    El aislamiento por tenant combina PostgreSQL RLS y validaciones explícitas
    en backend. El `tenant_id` se resuelve desde el contexto de autenticación.


    ## Flujo de Emisión


    1. `POST /documents` — reserva secuencial y encola el documento (responde
    202)

    2. `GET /documents/{id}` — consulta el estado actual por polling

    3. Cuando `status = AUTHORIZED`, el campo `authorization_number` contiene la
    clave de autorización del SRI


    Documentación completa en https://docs.factioapp.com
  version: 0.1.0
servers:
  - url: http://localhost:8000
    description: Local
  - url: https://api.factioapp.com
    description: Producción
  - url: https://api.staging.factioapp.com
    description: Sandbox (datos de prueba, sin efecto fiscal real)
security: []
paths:
  /auth/api-keys/{key_id}:
    patch:
      tags:
        - Authentication
      summary: Update Api Key
      description: >-
        Update an API key's name or scopes.


        This endpoint allows partial updates - only the fields provided in the
        request

        will be modified. The key_hash is never exposed in the response.


        Args:
            key_id: API key ID to update
            request: Partial update payload with optional name and/or scopes
            db: Async database session
            tenant_id: Active tenant ID
            current_user: Authenticated user (must be tenant_admin or tenant_owner)

        Returns:
            APIKeyResponse: Updated API key metadata (without key_hash)

        Raises:
            HTTPException 404: If API key not found or doesn't belong to tenant
            HTTPException 403: If user doesn't have permission
      operationId: update_api_key_auth_api_keys__key_id__patch
      parameters:
        - name: key_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
            title: Key Id
        - name: authorization
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: Authorization
        - name: x-api-key
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: X-Api-Key
        - name: x-tenant-id
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: X-Tenant-Id
        - name: factio_access_token
          in: cookie
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: Factio Access Token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/APIKeyUpdateRequest'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/APIKeyResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    APIKeyUpdateRequest:
      properties:
        name:
          anyOf:
            - type: string
              maxLength: 255
            - type: 'null'
          title: Name
          description: Human-readable name for the API key
        scopes:
          anyOf:
            - items:
                type: string
              type: array
            - type: 'null'
          title: Scopes
          description: List of scopes/permissions for this API key
      type: object
      title: APIKeyUpdateRequest
      description: Schema for updating an API key (partial update).
      example:
        name: Production Integration Key
        scopes:
          - documents:read
          - documents:write
    APIKeyResponse:
      properties:
        id:
          type: string
          format: uuid
          title: Id
        name:
          type: string
          title: Name
        key_prefix:
          type: string
          title: Key Prefix
        scopes:
          anyOf:
            - items:
                type: string
              type: array
            - type: 'null'
          title: Scopes
        is_active:
          type: boolean
          title: Is Active
        created_at:
          type: string
          format: date-time
          title: Created At
        last_used_at:
          anyOf:
            - type: string
              format: date-time
            - type: 'null'
          title: Last Used At
      type: object
      required:
        - id
        - name
        - key_prefix
        - scopes
        - is_active
        - created_at
        - last_used_at
      title: APIKeyResponse
      description: Schema for API key response (key_hash is never exposed).
      example:
        created_at: '2024-01-01T00:00:00Z'
        id: cc0e8400-e29b-41d4-a716-446655440000
        is_active: true
        key_prefix: fac_abc
        last_used_at: '2024-06-01T12:00:00Z'
        name: Production Integration Key
        scopes:
          - documents:read
          - documents:write
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
        input:
          title: Input
        ctx:
          type: object
          title: Context
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError

````