# Set Redis Key API

> Store one or several keys of your own in the Redis of API Maker with the set redis key system API - a value, a TTL in seconds, and NX to write only when the key is new.

Source: https://docs.apimaker.dev/v1/docs/apis-all/system-apis/system-generated-set-redis-key-api.html

Stores values in the external Redis of API Maker, in the namespace of the account : sessions, captchas, counters, anything a later call or another server needs. Each key can expire on its own.

| | |
|---|---|
| Method | POST |
| URL | `/api/system-api/admin/set-redis-key` : `admin` is the user path of your account |
| Body | an array of `{ key, value, ttl?, NX? }` |
| Answer | `data` : `true` |
| From code | [`g.sys.cache.setKey`](https://docs.apimaker.dev/v1/examples/sys/cache/setKey.html) |

## The call

**Body**

```json
[
    { "key": "session:user123", "value": "{\"cart\":3}", "ttl": 3600 },
    { "key": "captcha:abc", "value": "7H2K", "ttl": 180, "NX": true }
]
```

**Answer**

```json
{ "success": true, "statusCode": 200, "data": true }
```

| Key | Meaning |
|---|---|
| `key` | The name. It is stored under the namespace of the account. |
| `value` | A string. Serialise objects with `JSON.stringify`. |
| `ttl` | Seconds before the key expires. Without it, `redisValueExpireInSeconds` of the [configuration](https://docs.apimaker.dev/v1/docs/am-resources/api-maker-configurations.html) (7200 by default) : keys never live forever. |
| `NX` | `true` : write only when the key does not exist yet. |

## From code

```typescript
await g.sys.cache.setKey('session:user123', JSON.stringify({ cart: 3 }), 3600);
await g.sys.cache.setKey([ { key: 'captcha:abc', value: '7H2K', ttl: 180, NX: true } ]);
```

- The [Redis dashboard](https://docs.apimaker.dev/v1/docs/dashboard/redis-dashboard.html) shows the keys with their TTL and lets you edit them.

## Access and settings

- Over HTTP, a system API answers once its [settings](https://docs.apimaker.dev/v1/docs/settings/systemApiSettings.html) give it `apiAccessType: TOKEN_ACCESS` (the token of an API user whose [group](https://docs.apimaker.dev/v1/docs/apis-security/api-group-permission.html) grants this system API, in `x-am-authorization`) or `IS_PUBLIC`. Without settings it is `NO_ACCESS` : your code calls it through `g.sys`, the admin panel tests it, and an HTTP call is refused.
- The settings can also cache the answer or require person tokens (`authProviders`) ; [pre and post hooks](https://docs.apimaker.dev/v1/docs/apis-all/hooks/preHook-api.html) run around it like around any API.
- The [request headers](https://docs.apimaker.dev/v1/docs/apis-all/header/requestHeader.html) apply : `x-am-response-case`, `x-am-content-type-response`, `x-am-internationalization`, `x-am-tenant-username`…

## Errors

| Code | When |
|---|---|
| `400` | The body is wrong : the message names the missing or invalid key. |
| `401` | No valid API user token, or the API is `NO_ACCESS` : `You are not authorized to access this API.` |
| `403` | No group grants this system API. |

## Related

- [All APIs at a glance](https://docs.apimaker.dev/v1/docs/apis-all/overview.html) · [Response format](https://docs.apimaker.dev/v1/docs/apis-all/response-format.html) · [System API settings](https://docs.apimaker.dev/v1/docs/settings/systemApiSettings.html) · [System APIs from code](https://docs.apimaker.dev/v1/examples/sys/system/system.html)
