Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions docs/2_worker_tricks/HowTo_atomicCounter.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# HowTo: make an atomic counter using `countapi.xyz`?

This API allows you to create simple numeric counters. Each counter is identified inside a `namespace` with a `key`.

Keys and namespaces must have at least 3 characters and less or equal to 64. Keys and namespaces must match: **`^[A-Za-z0-9_\-.]{3,64}`** pattern.

## Prepare yourself 1: make a 64 char long private key

To create a unique key, for yourself, on your super safe computer,
Expand All @@ -21,12 +25,19 @@ Keep the KEY (and possibly also the DOMAIN) a secret.

To make a counter on `countapi.xyz`, replace namespace and key with your own values (demo values are `example.com` and `dvAV77q6uaIOSzE_cgq6Bs_q-vojyIglNLW8lWHtiGUuWM03mLCZnaWIqTtlWYhk`).

Use `/create?<namespace>&<key>` to make new counter.
Specify initial settings by adding optional params
* `value` The initial value stored.
* `enable_reset` Allows the key to be resetted with /set.
* `update_lowerbound` Restrict update to not subtract more than this number. This number must be negative or zero.
* `update_upperbound` Restrict update to not add more than this number. This number must be positive or zero.

`https://api.countapi.xyz/create?namespace=example.com&key=dvAV77q6uaIOSzE_cgq6Bs_q-vojyIglNLW8lWHtiGUuWM03mLCZnaWIqTtlWYhk&update_lowerbound=0&update_upperbound=1&value=64`

Check your counter by looking at its info on countapi.xyz:
Check your counter by looking at its info on countapi.xyz using `/info/<namespace>/<key>`:
`https://api.countapi.xyz/info/example.com/dvAV77q6uaIOSzE_cgq6Bs_q-vojyIglNLW8lWHtiGUuWM03mLCZnaWIqTtlWYhk`

You can use the counter by calling:
You can use the counter by calling `/hit/<namespace>/<key>`:
`https://api.countapi.xyz/hit/example.com/dvAV77q6uaIOSzE_cgq6Bs_q-vojyIglNLW8lWHtiGUuWM03mLCZnaWIqTtlWYhk`

The countapi.xyz returns json data.
Expand Down Expand Up @@ -62,4 +73,4 @@ addEventListener('fetch', e=>e.respondWith(handleRequest(e.request)));

## References

* [countapi.xyz](https://countapi.xyz)
* [countapi.xyz](https://countapi.xyz)