# Get Redis Key API

> Read one or several keys of your own from the Redis of API Maker with the get redis key system API - values set with set redis key, in the namespace of your account.

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

Reads values you stored with [set redis key](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-set-redis-key-api.html) from the external Redis of API Maker (the one which also holds the cache of the APIs). Keys are kept in a namespace of the account, so two accounts never see each other's keys.

| | |
|---|---|
| Method | POST |
| URL | `/api/system-api/admin/get-redis-key` : `admin` is the user path of your account |
| Body | `{ keys }` |
| Answer | `data` : the value (a string, `null` when the key is not there), or an array of values |
| From code | [`g.sys.cache.getKey`](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-get-redis-key-api.html#from-code) |

## The call

**Body : one key**

```json
{ "keys": "session:user123" }
```

**Answer**

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

**Body : several keys**

```json
{ "keys": [ "session:user123", "captcha:abc" ] }
```

**Answer**

```json
{ "success": true, "statusCode": 200, "data": [ "{\"cart\":3}", null ] }
```

- Values are strings : `JSON.parse` what you stored as JSON.

## From code

**TypeScript**

```typescript
const raw = await g.sys.cache.getKey('session:user123');
const session = raw ? JSON.parse(raw) : null;
```

**Python**

```python
import json

raw = await g.sys.cache.get_key('session:user123')
session = json.loads(raw) if raw else None
```

**PHP**

```php
$raw = $g->sys->cache->getKey('session:user123');
$session = $raw !== null ? json_decode($raw) : null;
```

**C#**

```csharp
var raw = await g.Sys.Cache.GetKey("session:user123");
var session = raw is null ? null : JsonNode.Parse(raw);
```

**Go**

```go
import "encoding/json"

raw, err := g.Sys.Cache.GetKey("session:user123")
if err != nil {
	return nil, err
}
var session any
if raw != "" {
	if err := json.Unmarshal([]byte(raw), &session); err != nil {
		return nil, err
	}
}
```

**Rust**

```rust
let raw = g.sys.cache.get_key("session:user123")?;
let session: Option<Value> = raw.map(|text| serde_json::from_str(&text)).transpose()?;
```

**Java**

```java
String raw = g.sys.cache.getKey("session:user123");
Object session = raw == null ? null : Json.parse(raw);
```

**Kotlin**

```kotlin
val raw = g.sys.cache.getKey("session:user123")
val session = raw?.let { Json.parse(it) }
```

### Several keys at once

- Get multiple redis keys values.

**TypeScript**

```typescript
await g.sys.cache.getKey(["REDIS_KEY_NAME", "ANOTHER_REDIS_KEY_NAME"]);
```

**Python**

```python
await g.sys.cache.get_keys(['REDIS_KEY_NAME', 'ANOTHER_REDIS_KEY_NAME'])
```

**PHP**

```php
$g->sys->cache->getKeys(['REDIS_KEY_NAME', 'ANOTHER_REDIS_KEY_NAME']);
```

**C#**

```csharp
await g.Sys.Cache.GetKeys(["REDIS_KEY_NAME", "ANOTHER_REDIS_KEY_NAME"]);
```

**Go**

```go
if _, err := g.Sys.Cache.GetKeys([]string{"REDIS_KEY_NAME", "ANOTHER_REDIS_KEY_NAME"}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.cache.get_keys(&["REDIS_KEY_NAME", "ANOTHER_REDIS_KEY_NAME"])?;
```

**Java**

```java
g.sys.cache.getKeys(List.of("REDIS_KEY_NAME", "ANOTHER_REDIS_KEY_NAME"));
```

**Kotlin**

```kotlin
g.sys.cache.getKeys(listOf("REDIS_KEY_NAME", "ANOTHER_REDIS_KEY_NAME"))
```

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