> For the complete documentation index, see [llms.txt](https://docs.msquared.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.msquared.io/creation/unreal-development/features-and-tutorials/web-services/walk-through-example/example-counter-service.md).

# Example Counter Service

This is some example code for the service we just tested. It is written in typescript and uses `express`.

```typescript

import express from "express";
import { expressjwt, Request as JWTRequest } from "express-jwt";
import jwksRsa from "jwks-rsa";

export class CounterModel {
  private _count: number;

  constructor() {
    this._count = 0;
  }

  get count() {
    return this._count;
  }

  increment() {
    this._count++;
  }

  decrement() {
    this._count--;
  }

  add(value: number) {
    this._count += value;
  }
}

const delegatedTokenAuth = expressjwt({
  secret: jwksRsa.expressJwtSecret({
    cache: true,
    rateLimit: true,
    jwksRequestsPerMinute: 5,
    jwksUri: "https://admin.m2worlds.io/.well-known/jwks.json",
  }) as any,
  audience: "scarcely-calm-lark",
  issuer: "scarcely-calm-lark:auth",
  algorithms: ["RS256"],
});


const app = express();

type CounterResponse = {
  counter: number;
};

type CounterUpdateRequest = {
  value: number;
};

// basic in memory counter
const counter = new CounterModel();

const userCounters = new Map<string, CounterModel>();

function findOrAddCounter(sub: string): CounterModel {
  let c = userCounters.get(sub);
  if (c) {
    return c;
  }

  c = new CounterModel();
  userCounters.set(sub, c);

  return c;
}

app.use(express.json());

app.head("/", (req, res) => {
  res.sendStatus(200);
});

app.get("/", (req, res) => {
  res.sendStatus(200);
});

// unauthenticated route
app.get<Record<string, never>, CounterResponse>("/api/counter", (req, res) => {
  res.send({ counter: counter.count });
});

// unauthenticated route
app.post<Record<string, never>, CounterResponse>("/api/counter", (req, res) => {
  counter.increment();
  res.send({ counter: counter.count });
});

// unauthenticated route
app.put<Record<string, never>, CounterResponse, CounterUpdateRequest>(
  "/api/counter",
  (req, res) => {
    counter.add(req.body.value);
    res.send({ counter: counter.count });
  }
);

// authenticated route
app.get<Record<string, never>, CounterResponse>(
  "/api/counter/me",
  delegatedTokenAuth,
  (req, res) => {
    const sub = (req as unknown as JWTRequest).auth?.sub;
    if (!sub) {
      res.status(401);
      return;
    }

    const c = findOrAddCounter(sub);

    res.send({ counter: c.count });
  }
);

// authenticated route
app.post<Record<string, never>, CounterResponse>(
  "/api/counter/me",
  delegatedTokenAuth,
  async (req, res) => {
    const sub = (req as unknown as JWTRequest).auth?.sub;
    if (!sub) {
      await res.status(401);
      return;
    }

    const c = findOrAddCounter(sub);

    c.increment();

    await res.send({ counter: c.count });
  }
);

export default app;

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.msquared.io/creation/unreal-development/features-and-tutorials/web-services/walk-through-example/example-counter-service.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
