# Storage, timers and HTTP

> A bot's private key-value storage, timers, and HTTPS requests to granted hosts.

These are host services. Each needs a capability in the [manifest](/docs/guide/bots/manifest-and-grants/)
that the owner granted; without it the call fails with `NOT_GRANTED`.

## Storage (`storage_bytes`)

A private key-value store per bot, kept on the host in its own database (never the community's) and
limited to the granted bytes.

**TypeScript:**

```ts
bot.storage.setJSON('counter', 42);
const n = bot.storage.getJSON<number>('counter');   // number | null
const keys = bot.storage.keys('user:', { limit: 100 });
bot.storage.delete('counter');
```

**Rust:**

```rust
use zaun_bot::storage;

storage::set("counter", &42u32.to_be_bytes())?;
let bytes = storage::get("counter")?;            // Option<Vec<u8>>
let keys = storage::list_keys("user:", None, 100)?;
storage::delete("counter")?;
```

`QUOTA_EXCEEDED` means the grant is used up. Data survives restarts and updates, moves with the
community's export, and is deleted when the bot is removed.

## Timers (`timers`)

**TypeScript:**

```ts
const id = bot.timers.after(60_000, 'reminder:42');  // once, in 60 s
const tick = bot.timers.every(3_600_000, 'hourly');  // repeating, at least every second
bot.timers.cancel(id);
bot.on('timer', ({ id, tag }) => bot.log.info('timer', id, tag));
```

`setTimeout` and `setInterval` never fire in a bot: nothing runs between calls. Use these timers.

**Rust:**

```rust
use zaun_bot::timers;

let id = timers::after(60_000, "reminder:42")?;  // once, in 60 s
let tick = timers::every(3_600_000, "hourly")?;  // repeating, at least every second
timers::cancel(id);
```

`Bot::on_timer(id, tag)` runs when one fires.

Timers don't survive a restart: store what matters and arm timers again when the bot starts (in
`init`/`ready` in TypeScript, in `commands()` in Rust).

## HTTP (`network`)

**TypeScript:**

```ts
const res = bot.http.fetch('https://api.open-meteo.com/v1/forecast?latitude=30&longitude=31', { timeoutMs: 5_000 });
const forecast = res.json<{ current?: unknown }>();   // HttpError (kind 'not-granted', …) when refused
```

There is no global `fetch` in a bot; this is the only way out.

**Rust:**

```rust
use zaun_bot::http::{fetch, Request};

let res = fetch(&Request {
    method: "GET".into(),
    url: "https://api.open-meteo.com/v1/forecast?latitude=30&longitude=31".into(),
    headers: vec![],
    body: None,
    timeout_ms: Some(5_000),
})?;
```

- Only `https://` URLs, to hosts in both the bot's grant and the operator's ceiling.
- The host resolves the name and **refuses loopback, private, link-local and reserved addresses**, so
  a bot can't reach the host's own network.
- Requests leave from the host: **the remote server learns the host's IP address**, never a
  member's. Owners see the list of hosts on the bot card before granting it.
- Responses are limited in size (4 MiB) and time.
- Bots have no sockets: this is the only way out.

> **Note: State can vanish**
>
> A bot instance may restart at any time (an update, a crash, a host restart). Keep anything that must
> survive in storage, not in variables or struct fields.
