Skip to content

Custom hooks

Paid feature

Define named, server-side operations you invoke like any other method — redis.topUsers(...). Hooks are pre-registered from your dashboard and validated on every call, so only operations you approve can run.

How it works

  1. Register a hook in your dashboard under a service. Give it a name, a kind, and a body.
  2. Call it by name from any client. Zedgi checks the name is registered for that service and that your balance is positive.
  3. The proxy executes your hook server-side and returns the result.

WARNING

Unregistered names are rejected with ZEDGI_HOOK_NOT_FOUND. Custom hooks require a positive balance — on the free tier they return ZEDGI_PAID_FEATURE (402). Built-in methods always work on the free tier.

Two kinds of hook

  • script — A Lua script (Redis, run via EVALSHA) or a parameterized SQL template (Postgres/MySQL).
  • macro — An ordered list of built-in commands/statements run as a pipeline or transaction, with {0}, {1} argument substitution.

Redis Lua hook

Registered as topUsers, kind script:

lua
return redis.call('ZREVRANGE', KEYS[1], 0, tonumber(ARGV[1]) - 1)

Invoke it:

ts
const redis = zedgi.redis();
// keys → KEYS, args → ARGV
await redis.hook('topUsers', { keys: ['leaderboard'], args: [10] });
python
redis = zedgi.redis()
redis.hook('topUsers', keys=['leaderboard'], args=[10])
http
POST /rpc HTTP/1.1
Host: dev123.zedgi.app
x-zedgi-key: zk_live_xxx
content-type: application/json

{
  "service": "redis",
  "method": "topUsers",
  "payload": { "keys": ["leaderboard"], "args": [10] }
}

SQL template hook

Registered as activeUsers, kind script:

sql
SELECT id, email FROM users WHERE last_seen > NOW() - ($1 || ' days')::interval

Invoke it:

ts
const pg = zedgi.postgres();
await pg.hook('activeUsers', { params: [30] });
python
pg = zedgi.postgres()
pg.hook('activeUsers', params=[30])
http
POST /rpc HTTP/1.1
Host: dev123.zedgi.app
x-zedgi-key: zk_live_xxx
content-type: application/json

{
  "service": "postgres",
  "method": "activeUsers",
  "payload": { "params": [30] }
}

Macro hook

Registered as cacheUser, kind macro, body:

json
[
  { "command": "SET", "args": ["user:{0}", "{1}"] },
  { "command": "EXPIRE", "args": ["user:{0}", 3600] }
]

Invoke it — positional args fill {0}, {1}:

ts
const redis = zedgi.redis();
// {0} = '42', {1} = jsonStr
await redis.cacheUser('42', jsonStr);
python
redis = zedgi.redis()
redis.hook('cacheUser', args=['42', json_str])
http
POST /rpc HTTP/1.1
Host: dev123.zedgi.app
x-zedgi-key: zk_live_xxx
content-type: application/json

{
  "service": "redis",
  "method": "cacheUser",
  "payload": { "args": ["42", "<json>"] }
}