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

# Get Or Create Api Key For User

> Get or create an API key for a user on behalf of the automations service.

If a key with the given name already exists for the user/org and is not expired,
returns the existing key. Otherwise, creates a new key.

The created/returned keys are system keys and are:
- Not visible to the user in their API keys list
- Not deletable by the user
- Never expire

Args:
    user_id: The user ID
    org_id: The organization ID
    request: Request body containing name (required)
    x_service_api_key: Service API key header for authentication

Returns:
    CreateUserApiKeyResponse: The API key and metadata

Raises:
    HTTPException: 401 if service key is invalid
    HTTPException: 404 if user not found
    HTTPException: 403 if user is not a member of the specified org



## OpenAPI

````yaml /openapi/openhands-cloud.json post /api/service/users/{user_id}/orgs/{org_id}/api-keys
openapi: 3.1.0
info:
  title: OpenHands
  description: 'OpenHands: Code Less, Make More'
  version: 0.0.1
servers: []
security: []
paths:
  /api/service/users/{user_id}/orgs/{org_id}/api-keys:
    post:
      tags:
        - Service
      summary: Get Or Create Api Key For User
      description: >-
        Get or create an API key for a user on behalf of the automations
        service.


        If a key with the given name already exists for the user/org and is not
        expired,

        returns the existing key. Otherwise, creates a new key.


        The created/returned keys are system keys and are:

        - Not visible to the user in their API keys list

        - Not deletable by the user

        - Never expire


        Args:
            user_id: The user ID
            org_id: The organization ID
            request: Request body containing name (required)
            x_service_api_key: Service API key header for authentication

        Returns:
            CreateUserApiKeyResponse: The API key and metadata

        Raises:
            HTTPException: 401 if service key is invalid
            HTTPException: 404 if user not found
            HTTPException: 403 if user is not a member of the specified org
      operationId: >-
        get_or_create_api_key_for_user_api_service_users__user_id__orgs__org_id__api_keys_post
      parameters:
        - name: user_id
          in: path
          required: true
          schema:
            type: string
            title: User Id
        - name: org_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
            title: Org Id
        - name: X-Service-API-Key
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: X-Service-Api-Key
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateUserApiKeyRequest'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateUserApiKeyResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    CreateUserApiKeyRequest:
      properties:
        name:
          type: string
          title: Name
      type: object
      required:
        - name
      title: CreateUserApiKeyRequest
      description: Request model for creating an API key on behalf of a user.
    CreateUserApiKeyResponse:
      properties:
        key:
          type: string
          title: Key
        user_id:
          type: string
          title: User Id
        org_id:
          type: string
          title: Org Id
        name:
          type: string
          title: Name
      type: object
      required:
        - key
        - user_id
        - org_id
        - name
      title: CreateUserApiKeyResponse
      description: Response model for created API key.
    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

````