# Hit Counter and Badges API

> Count page views, downloads or events per key with atomic increments, unique-visitor windows and daily history, and show the value as a public SVG badge.

- Endpoint: `GET | POST | PATCH | DELETE https://apibox.io.vn/v1/counter/{namespace}/{key}/hit`
- Category: Web & SEO
- HTML docs: https://apibox.io.vn/docs/counter/ (Vietnamese: https://apibox.io.vn/vi/docs/counter/)
- Online tool: https://apibox.io.vn/tools/counter/
- Authentication: `Authorization: Bearer <API key>` or `X-API-Key: <API key>` (free key: https://apibox.io.vn/dashboard/#signup)

Counters belong to your account and live under a namespace: `POST /v1/counter/{namespace}/{key}/hit` adds 1 and returns the new `value` (the counter is created on the first hit), `/increment?by=5` and `/decrement` change it by any amount up to 1 000 000, `/set` (`value`) and `/reset` (optionally `clear_history=true`) overwrite it, `GET /v1/counter/{namespace}/{key}` reads it with `daily` hits for the last `days` (default 30, up to 365), `PATCH` changes options and `DELETE` removes it. `GET /v1/counter` lists your counters, `GET /v1/counter/{namespace}` one namespace with its total, and `POST /v1/counter` creates one explicitly with a start `value`. Increments are atomic, so concurrent hits are never lost.

With `unique_window_s` (up to 30 days) a hit counts once per visitor per window: the visitor is your `visitor` value (a session or user id) or the request IP, stored only as a short keyed hash that expires with the window; `hits_total` keeps every hit. With `public_read=true` the counter gets a public, read-only badge at `https://apibox.io.vn/c/{id}.svg` (shields-style; `label`, `color`, `style=flat-square`, `number_format=full|plain`) and JSON at `/c/{id}.json`: no API key, cached for 5 minutes, not counted in your quota. `?preview=badge&value=1234&label=views` renders a badge without an account.

Every /v1 call — including each hit — counts as one request of your monthly quota, so call `hit` from your server or page script for real events and use the free badge URL to display the number. Plans: Free 20 counters, Starter 500, Pro 5 000.

## Parameters

| Name | In | Type | Required | Default | Description |
|---|---|---|---|---|---|
| `namespace` | path | string | yes |  | Group name: 1-64 letters, digits, "_", "-", ".". |
| `key` | path | string | yes |  | Counter name (same characters). Followed by /hit, /increment, /decrement, /set or /reset. |
| `by` | query|body | number | no | `1` | increment / decrement: amount (1-1 000 000). |
| `value` | query|body | number | no |  | set / reset / create: new value. |
| `visitor` | query|body | string | no |  | Visitor id for unique counting (default: request IP), hashed. |
| `unique_window_s` | query|body | number | no | `0` | Count each visitor once per window (0 = every hit, max 2 592 000). |
| `public_read` | query|body | boolean | no | `false` | Enable the public badge /c/{id}.svg and /c/{id}.json. |
| `days` | query | number | no | `30` | GET: days of daily history (0-365). |
| `clear_history` | body | boolean | no | `false` | reset: also delete the daily history. |
| `preview` | query | string | no |  | `badge`: render a badge from `value`, `label`, `color`, `style` without an account. |

## Example request

```bash
curl -X POST "https://apibox.io.vn/v1/counter/blog/hello-world/hit" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

## Example response

```json
{
  "id": "cnt_lqeuenf28r8b7my7",
  "namespace": "blog",
  "key": "hello-world",
  "value": 2,
  "public_read": true,
  "unique_window_s": 86400,
  "hits_total": 3,
  "created_at": "2026-11-11T10:47:43.000Z",
  "updated_at": "2026-11-11T10:47:43.000Z",
  "last_hit_at": "2026-11-11T10:47:43.000Z",
  "badge_url": "https://apibox.io.vn/c/cnt_lqeuenf28r8b7my7.svg",
  "json_url": "https://apibox.io.vn/c/cnt_lqeuenf28r8b7my7.json",
  "counted": false,
  "note": "Already counted for this visitor within 86400 s"
}
```

## Use cases

- Show a views or downloads badge in a GitHub README, blog or Notion page
- Count page views of a static site, landing page or documentation without analytics
- Count file downloads, button clicks, votes or game plays
- Keep simple per-day event statistics for a dashboard or sparkline

## FAQ

### Does showing the badge use my quota?

No. The badge and JSON under /c/ are free and cached 5 minutes. Only /v1/counter calls (hit, get, reset…) count.

### Can visitors increase the counter without my key?

No. Hits need your API key, so call hit from your backend (or a serverless function) and keep the key secret.

### Is the IP address stored?

No. Only a truncated keyed hash is kept for unique counting, and it is deleted when the window ends.
