This page takes you from an empty folder to a running API. It should take a couple of minutes.

Requirements

  • Node.js and npm
  • A Cloudflare account on the paid Workers plan — Apiker stores data in Durable Objects, which are not available on the free plan
  • Wrangler, Cloudflare's CLI, which the generated project installs for you

Create a project

The quickest start is the scaffolder. It clones the project template, installs dependencies and leaves you with a working API:

npx apiker my-api cd my-api

To add Apiker to a project you already have, install it directly:

npm install apiker

Write your first route

A route maps a path to a handler. The handler receives the request context and returns a response built with one of the res helpers.

src/index.ts
JavaScript
1import { apiker, res } from "apiker";
2
3const routes = {
4 "/hello": () => res("Hello World!"),
5 "/users/:id": ({ matches }) => res({ id: matches.params.id })
6};
7
8apiker.init({
9 routes,
10 exports,
11 objects: ["Common"]
12});

Three options matter here. routes is your API. exports is the module's exports object, which Apiker attaches the Worker fetch handler and your Durable Object classes to. objects lists the Durable Objects the API stores data in — every project needs at least Common.

Run it locally

From inside the project directory (my-api, or wherever you installed apiker):

my-api/
Bash/Shell
npx wrangler dev

Then call it:

curl http://localhost:8787/users/my-user { "id": "my-user" }

Store something

Handlers receive a state function that reads and writes a Durable Object. Values persist across requests and deployments.

1const routes = {
2 "/users/:id": async ({ matches, state, body, request }) => {
3 const id = matches.params.id;
4
5 if (request.method === "POST") {
6 await state("Users", id).put({ profile: body });
7 return res_201("Saved");
8 }
9
10 return res(await state("Users", id).get("profile"));
11 }
12};
13
14apiker.init({ routes, exports, objects: ["Common", "Users"] });
Every Durable Object you use must be listed in objects, or the state call has nowhere to go.

Next steps