Skip to content
This page View Markdown Open in ChatGPT Open in Claude

Set redis key

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

The call

Body
[
    { "key": "session:user123", "value": "{\"cart\":3}", "ttl": 3600 },
    { "key": "captcha:abc", "value": "7H2K", "ttl": 180, "NX": true }
]
Answer
{ "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 (7200 by default) : keys never live forever.
NX true : write only when the key does not exist yet.

From code

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 } ]);
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)])
$g->sys->cache->setKey('session:user123', json_encode(['cart' => 3]), 3600);
$g->sys->cache->setKeys([new SetKey('captcha:abc', '7H2K', ttl: 180, nx: true)]);
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 }]);
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
}
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)])?;
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)));
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 shows the keys with their TTL and lets you edit them.

Access, headers and errors

  • Over HTTP, a system API answers only once its settings open it : TOKEN_ACCESS (the token of an API user whose group grants this system API) or IS_PUBLIC. Without settings it is NO_ACCESS : only your code (g.sys) and the admin panel reach it.
  • The settings can also keep the answer in the cache, or ask for the token of a person (authProviders). Pre and post hooks run around it, as around any API.
  • It takes the request headers and answers the envelope. 401 : no valid token, or the API is NO_ACCESS. 403 : no group grants it. 400 : the body is wrong, and the message names the key.

Related : All APIs · System API settings · System APIs from code