# 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/v4/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/v4/docs/apis-all/system-apis/system-generated-set-redis-key-api.html#from-code) |

## 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/v4/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**

```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 } ]);
```

**Python**

```python
import json

await g.sys.cache.set_key('session:user123', json.dumps({'cart': 3}), 3600)
await g.sys.cache.set_keys([SetKey('captcha:abc', '7H2K', ttl=180, nx=True)])
```

**PHP**

```php
$g->sys->cache->setKey('session:user123', json_encode(['cart' => 3]), 3600);
$g->sys->cache->setKeys([new SetKey('captcha:abc', '7H2K', ttl: 180, nx: true)]);
```

**C#**

```csharp
await g.Sys.Cache.SetKey("session:user123", JsonSerializer.Serialize(new { cart = 3 }), 3600);
await g.Sys.Cache.SetKeys([new SetKey("captcha:abc", "7H2K") { Ttl = 180, NX = true }]);
```

**Go**

```go
import "encoding/json"

session, err := json.Marshal(am.M{"cart": 3})
if err != nil {
	return nil, err
}
if _, err := g.Sys.Cache.SetKey("session:user123", string(session), 3600); err != nil {
	return nil, err
}
if _, err := g.Sys.Cache.SetKeys([]am.SetKey{
	{Key: "captcha:abc", Value: "7H2K", Ttl: 180, NX: true},
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.cache.set_key("session:user123", &json!({ "cart": 3 }).to_string(), Some(3600))?;
g.sys.cache.set_keys(vec![SetKey::new("captcha:abc", "7H2K").ttl(180).nx(true)])?;
```

**Java**

```java
g.sys.cache.setKey("session:user123", Json.stringify(Map.of("cart", 3)), 3600);
g.sys.cache.setKeys(List.of(new SetKey("captcha:abc", "7H2K").ttl(180).nx(true)));
```

**Kotlin**

```kotlin
g.sys.cache.setKey("session:user123", Json.stringify(mapOf("cart" to 3)), 3600)
g.sys.cache.setKeys(listOf(SetKey("captcha:abc", "7H2K").ttl(180).nx(true)))
```

- The [Redis dashboard](https://docs.apimaker.dev/v4/docs/logs/logs-and-dashboards.html#redis) 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/v4/docs/settings/systemApiSettings.html) give it `apiAccessType: TOKEN_ACCESS` (the token of an API user whose [group](https://docs.apimaker.dev/v4/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/v4/docs/apis-all/hooks/preHook-api.html) run around it like around any API.
- The [request headers](https://docs.apimaker.dev/v4/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/v4/docs/apis-all/overview.html) · [Response format](https://docs.apimaker.dev/v4/docs/apis-all/response-format.html) · [System API settings](https://docs.apimaker.dev/v4/docs/settings/systemApiSettings.html) · [System APIs from code](https://docs.apimaker.dev/v4/examples/sys/system/system.html)
