# Create key store

> Source: https://truto.one/docs/api-reference/admin/key-stores/create/

`POST /key-store`

Resource: **Key stores**

Connect a customer-owned secret store to an environment. Team admins only (signed-in users; API tokens get 403). The credentials are checked with the store first (400 if it rejects them). The key store starts `connecting`: give the credentials the permissions from `GET /key-store/{id}/setup`, then call `POST /key-store/{id}/verify`. An environment can have many key stores; returns 409 if another of its stores already uses the same location (for AWS: the account, region and secret name prefix), since they would share every secret.

## Request body

- **`environment_id`** _(string)_
- **`name`** _(string)_
- **`provider`** _(string)_
  Allowed: `aws_secrets_manager`
- **`config`** _(object)_
  Fields per provider: `GET /key-store-provider`. For `aws_secrets_manager`: `region` (required), `secret_name_prefix` and `kms_key_id` (optional).
- **`credentials`** _(object)_
  How Truto reaches the store, kept encrypted and never returned. For `aws_secrets_manager`: `access_key_id` and `secret_access_key` of an IAM user that has the policy from `GET /key-store/{id}/setup`.

## Response body

- **`id`** _(string)_
- **`environment_id`** _(string)_
  The environment whose secrets live in this store. An environment can have many key stores, at most one `active`.
- **`name`** _(string)_
- **`provider`** _(string)_
  The store. `aws_secrets_manager`: AWS Secrets Manager, with secrets encrypted by the customer's KMS key when `config.kms_key_id` is set.
  Allowed: `aws_secrets_manager`
- **`config`** _(object)_
  Non-secret settings; the fields per provider are listed by `GET /key-store-provider`. For `aws_secrets_manager`: `region`, `secret_name_prefix` (default `truto/`; each account is stored at `<prefix><environment_id>/<integrated_account_id>`, its `integration_override` at `<prefix><environment_id>/<integrated_account_id>/integration-override`, and an environment integration's `override` at `<prefix><environment_id>/environment-integration/<environment_integration_id>`), an optional `kms_key_id`, and `account_id` / `partition`, which Truto reads from AWS for the store's credentials. The store's `credentials` are write-only and never returned.
- **`status`** _(string)_
  Whether the store works, like an integrated account's status. `connecting`: created, or its settings changed while it holds nothing; not proven yet, and nothing is written to it. `active`: the last `POST /key-store/{id}/verify` passed. `validation_error`: the last verification failed (why is in `last_error`); a verification that passes sets it back to `active`. Which store is used is `is_current`.
  Allowed: `connecting`, `active`, `validation_error`
- **`is_current`** _(boolean)_
  Whether this is the environment's current key store (at most one): new and updated secrets in the environment are written to it; existing ones move on their next write, or all at once with `POST /key-store/{id}/move-secrets`. A store that isn't current keeps serving what is already in it. A current store whose verification fails stays current: its secrets are only there.
- **`last_verified_at`** _(string)_
  When verification last succeeded (UTC).
- **`last_error`** _(string)_
  Why the last verification failed.
- **`deactivated_at`** _(string)_
  When the key store last stopped being current (UTC). It can be deleted a minute after this. Null if it was never current.
- **`created_at`** _(string)_
- **`updated_at`** _(string)_

## Errors

| Status | Description |
| :----- | :---------- |
| `409` | Another key store of the environment uses the same location. |

## Code examples

### curl

```bash
curl -X POST 'https://api.truto.one/key-store' \
  -H 'Authorization: Bearer <your_api_token>' \
  -H 'Content-Type: application/json' \
  -d '{
  "environment_id": "your_environment_id",
  "name": "your_name",
  "provider": "aws_secrets_manager",
  "config": {},
  "credentials": {}
}'
```

### JavaScript

```javascript
const body = {
  "environment_id": "your_environment_id",
  "name": "your_name",
  "provider": "aws_secrets_manager",
  "config": {},
  "credentials": {}
};

const response = await fetch('https://api.truto.one/key-store', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <your_api_token>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(body),
});

const data = await response.json();
console.log(data);
```

### Python

```python
import requests

url = "https://api.truto.one/key-store"
headers = {
    "Authorization": "Bearer <your_api_token>",
    "Content-Type": "application/json",
}
params = {
}
payload = {
    "environment_id": "your_environment_id",
    "name": "your_name",
    "provider": "aws_secrets_manager",
    "config": {},
    "credentials": {}
}

response = requests.post(url, headers=headers, params=params, json=payload)
print(response.json())
```
