# Durable Counter (/experiments/durable-counter)



A minimal reference for **Durable Objects**: a single `Counter` class with persistent storage, exposed via HTTP routes. Use this pattern when you need strongly consistent state at the edge (rate limits, sessions, coordination).

## API Endpoints [#api-endpoints]

### GET /counter [#get-counter]

Returns the current counter value.

#### Example Request [#example-request]

```bash
curl "https://your-worker.workers.dev/counter"
```

#### Example Response [#example-response]

```json
{ "value": 42 }
```

### POST /counter/increment [#post-counterincrement]

Increments the counter by 1.

```bash
curl -X POST "https://your-worker.workers.dev/counter/increment"
```

### POST /counter/reset [#post-counterreset]

Resets the counter to 0.

```bash
curl -X POST "https://your-worker.workers.dev/counter/reset"
```

## Use Cases [#use-cases]

* Learn how to define and export a Durable Object class
* Shared counters or rate limiters across all edge locations
* Starting point for session storage or real-time coordination
* Understanding `idFromName()` for addressing a single global object

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/durable-counter)
  </Step>

  <Step>
    ### Test the API [#test-the-api]

    ```bash
    curl "https://your-worker.workers.dev/counter"
    curl -X POST "https://your-worker.workers.dev/counter/increment"
    ```
  </Step>
</Steps>

## Local Development [#local-development]

```bash
cd apps/experiments/durable-counter
npm install
npm run dev
```

## Configuration [#configuration]

`wrangler.json` declares:

* **Durable Object binding** `COUNTER` → class `Counter`
* **Migration** `v1` registering the `Counter` class

## Cloudflare Features Used [#cloudflare-features-used]

* [Durable Objects](https://developers.cloudflare.com/durable-objects/) - strongly consistent state and storage
* [Workers](https://developers.cloudflare.com/workers/) - HTTP routes that call into the DO stub
