> ## 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.

# kosli_service_account_api_keys data source

> Lists the active API keys for a Kosli service account. Returns key metadata only, for auditing keys and building rotation logic.

Lists the active API keys for a Kosli service account. Only key metadata is returned. The raw key value is never retrievable after creation.

Use this data source to:

* Audit the active API keys of a service account
* Alert on keys nearing their `expires_at`
* Find keys that have never been used (`last_used_at` is null) as candidates for cleanup

> [!NOTE]
>
> To mint a new key, use the [`kosli_service_account_api_key` resource](https://docs.kosli.com/terraform-reference/resources/service_account_api_key). All timestamps are RFC3339 UTC strings. `expires_at` is null for keys that never expire, and `last_used_at` is null for keys that have never been used.

## Example usage

```terraform theme={"theme":"dracula","languages":{"custom":["/languages/rego.json"]}}
terraform {
  required_providers {
    kosli = {
      source = "kosli-dev/kosli"
    }
  }
}

# A service account with an API key
resource "kosli_service_account" "ci" {
  name      = "ci-pipeline"
  privilege = "member"
}

resource "kosli_service_account_api_key" "ci_key" {
  service_account_name = kosli_service_account.ci.name
  description          = "Production CI key"
}

# List all active API keys for the service account (metadata only; the raw key
# value is never returned after creation).
data "kosli_service_account_api_keys" "ci" {
  service_account_name = kosli_service_account.ci.name

  depends_on = [kosli_service_account_api_key.ci_key]
}

# Example: surface keys that have never been used, for rotation/cleanup.
output "unused_api_key_ids" {
  description = "IDs of API keys that have never been used"
  value       = [for k in data.kosli_service_account_api_keys.ci.keys : k.id if k.last_used_at == null]
}
```

## Schema

### Required

* `service_account_name` (String) The name of the service account whose API keys should be listed.

### Read-only

* `keys` (Attributes List) The list of active API keys for the service account, sorted by creation time (oldest first). See the nested schema below.

### Nested schema for `keys`

Read-only:

* `created_at` (String) RFC3339 UTC timestamp of when the API key was created.
* `description` (String) Description of the API key.
* `expires_at` (String) RFC3339 UTC timestamp at which the key expires. Null if the key never expires.
* `id` (String) Server-assigned identifier of the API key.
* `last_used_at` (String) RFC3339 UTC timestamp of when the API key was last used. Null if the key has never been used.


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