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

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.kosli.com/feedback

```json
{
  "path": "/api-reference/service-accounts/create-an-api-key-for-a-service-account",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Create an API key for a service account

> Create a new API key for a service account. The key value is only returned once.



## OpenAPI

````yaml https://app.kosli.com/api/v2/openapi.json post /service-accounts/{org}/{name}/api-keys
openapi: 3.1.0
info:
  title: Kosli API
  summary: The API for communicating with Kosli
  description: >

    # Authentication 


    When making requests against Kosli API, you can authenticate your requests
    using a bearer token. 

    Set the bearer token in the request Authorization header to a valid API
    key. 

    API Keys can be personal or for service accounts. Check the [service
    accounts
    documentation](https://docs.kosli.com/getting_started/service-accounts/) for
    details. 


    ## Curl example


    ```shell

    curl -H "Authorization: Bearer <<your-api-key>>"
    https://app.kosli.com/api/v2/environments/<<your-org-name>>

    ```
  version: '2.0'
servers:
  - url: https://app.kosli.com/api/v2
    description: EU
  - url: https://app.us.kosli.com/api/v2
    description: US
security:
  - HTTPBearer: []
paths:
  /service-accounts/{org}/{name}/api-keys:
    post:
      tags:
        - Service Accounts
      summary: Create an API key for a service account
      description: >-
        Create a new API key for a service account. The key value is only
        returned once.
      operationId: >-
        create_service_account_api_key_service_accounts__org___name__api_keys_post
      parameters:
        - name: name
          in: path
          required: true
          schema:
            type: string
            title: Name
        - name: org
          in: path
          required: true
          schema:
            type: string
            title: Org
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ApiKeyPostInput'
      responses:
        '201':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKeyCreatedResponse'
        '400':
          description: Invalid expiry date
          content:
            application/json:
              schema:
                $ref: >-
                  #/components/schemas/fastapi_app__errors__ValidationErrorResponse___locals___ValidationErrorResponseModel__15
        '403':
          description: Forbidden
          content:
            application/json:
              schema:
                $ref: >-
                  #/components/schemas/fastapi_app__errors__ForbiddenResponse___locals___ForbiddenResponseModel__6
        '404':
          description: Service account not found
          content:
            application/json:
              schema:
                $ref: >-
                  #/components/schemas/fastapi_app__errors__NotFoundResponse___locals___NotFoundResponseModel__41
      security:
        - HTTPBearer: []
        - HTTPBasic: []
components:
  schemas:
    ApiKeyPostInput:
      properties:
        description:
          type: string
          minLength: 1
          title: Description
        expires_at:
          anyOf:
            - type: integer
            - type: 'null'
          title: Expires At
          description: >-
            Unix timestamp for key expiry. Use 0 or omit for no expiry. Must not
            be in the past.
      type: object
      required:
        - description
      title: ApiKeyPostInput
    ApiKeyCreatedResponse:
      properties:
        id:
          type: string
          title: Id
        key:
          type: string
          title: Key
        description:
          type: string
          title: Description
        created_at:
          type: number
          title: Created At
        expires_at:
          type: integer
          title: Expires At
      type: object
      required:
        - id
        - key
        - description
        - created_at
        - expires_at
      title: ApiKeyCreatedResponse
    fastapi_app__errors__ValidationErrorResponse___locals___ValidationErrorResponseModel__15:
      properties:
        message:
          type: string
          title: Message
          description: Error message
        errors:
          anyOf:
            - additionalProperties:
                type: string
              type: object
            - type: 'null'
          title: Errors
          description: Validation errors by field
      type: object
      required:
        - message
      title: ValidationErrorResponseModel
      description: expires_at cannot be in the past
      examples:
        - errors:
            email: Field must be a valid email address
            name: Field cannot be empty
          message: expires_at cannot be in the past
    fastapi_app__errors__ForbiddenResponse___locals___ForbiddenResponseModel__6:
      properties:
        message:
          type: string
          title: Message
          description: Error message
        errors:
          anyOf:
            - additionalProperties:
                type: string
              type: object
            - type: 'null'
          title: Errors
          description: Validation errors by field
      type: object
      required:
        - message
      title: ForbiddenResponseModel
      examples:
        - message: You don't have permission to access this resource
    fastapi_app__errors__NotFoundResponse___locals___NotFoundResponseModel__41:
      properties:
        message:
          type: string
          title: Message
          description: Error message
        errors:
          anyOf:
            - additionalProperties:
                type: string
              type: object
            - type: 'null'
          title: Errors
          description: Validation errors by field
      type: object
      required:
        - message
      title: NotFoundResponseModel
      description: Service account not found
      examples:
        - message: Service account not found
  securitySchemes:
    HTTPBearer:
      type: http
      scheme: bearer

````