Files
sweet/docs/latest/examples/creating-a-crud-api.md
T
Christian Svensson a2de5154ec fix(docs): improve and increase consistency for code blocks (#3163)
Follow-up from #3162.

This PR does various tweaks, mostly related to code blocks.

- Adds missing titles to code blocks, and updates titles to be more
consistent
- Adds filenames to most ` ```tsx` blocks with e.g. `main.ts`, `dev.ts`
or a suitable example (e.g. `routes/greeting.tsx`) based on the code
- Adds filenames to ` ```diff` blocks if there's a relevant file, to
make the icon e.g. TS over Diff icon
  - Code blocks with Fresh code (e.g. interfaces) uses ` ```tsx fresh 🍋`
- Changes most ` ```sh` blocks with example terminal commands to ` ```sh
Terminal`
- Updates ` ```txt Project Structure` or ` ```diff Project Structure`
blocks to be more consistent
- Changes `sh` or `txt` to `txt-files` which improves highlighting and
makes icon consistent
- Renames `<project dir>` to `<project root>` to match builder `root`
option terminology
- Adds `<project root>` root folder to most/all overview folder
overviews
  - Makes the structure blocks more consistently displayed
- Tried to make some "bare" examples more complete

## Comparison

Here's a small curated set of before/after changes.

### Deployment

- `deno` highlighted in terminal commands
- `.gitignore` syntax highlighting works without using `sh` as language

<img width="1641" height="1009" alt="image"
src="https://github.com/user-attachments/assets/170cf833-1f6a-4b0d-8d2d-a081bc28a967"
/>

### File routing

Updated icon, comments same as before, but root and hierarchy have new
colors (`operator`/`function`).

<img width="1647" height="588" alt="image"
src="https://github.com/user-attachments/assets/f28db51b-4bee-45e4-be1c-e137a6d3ae3d"
/>
2025-08-01 09:07:42 +02:00

253 lines
6.9 KiB
Markdown

---
description: |
Use HTTP CRUD methods to perform operations on resources. Learn how to use HTTP handlers to create a RESTful API.
---
The MDN [docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) are a
great resource to learn more about HTTP methods. We'll touch on the four
fundamental methods necessary to create a basic CRUD (create, read, update,
delete) API here. Additionally, we'll briefly mention CORS requests and how
`OPTIONS` comes into play.
Using HTTP methods is a common way to create a REST API. Fresh supports common
HTTP methods in handlers out of the box. Async HTTP requests are also supported.
Read more about custom handlers [here](/docs/getting-started/custom-handlers).
In this example we'll be creating a small API that uses
[Deno KV](https://deno.com/kv) to store users in a database.
Our project structure will look like this (in addition to the rest of the Fresh
code from a new project):
```txt-files Project Structure
<project root>
└── routes
└── api
└── users
├── [id].ts
└── index.ts
```
In each section about a method, only the relevant handler will be shown. The
full files are available at the bottom for reference.
## POST
`POST` (create) is used to create a resource.
```tsx routes/api/users/index.ts
export const handler: Handlers<User | null> = {
async POST(req, _ctx) {
const user = (await req.json()) as User;
const userKey = ["user", user.id];
const ok = await kv.atomic().set(userKey, user).commit();
if (!ok) throw new Error("Something went wrong.");
return new Response(JSON.stringify(user));
},
};
```
Test this with Postman (or your favorite client) with a URL like
`http://localhost:8000/api/users` and a method of `POST`. Make sure to have a
payload like:
```json Request body
{
"id": "2",
"name": "TestUserName"
}
```
You should receive the same thing back:
```json Response body
{ "id": "2", "name": "TestUserName" }
```
## GET
`GET` (read) is used to retrieve a resource and is by far the most common HTTP
method. You can use `GET` to fetch database content, markdown, or static files.
```tsx routes/api/users/[id].ts
export const handler: Handlers<User | null> = {
async GET(_req, ctx) {
const id = ctx.params.id;
const key = ["user", id];
const user = (await kv.get<User>(key)).value!;
return new Response(JSON.stringify(user));
},
};
```
Let's practice retrieving our user! A `GET` request to
`http://localhost:8000/api/users/2` should return:
```json Response body
{ "id": "2", "name": "TestUserName" }
```
## PUT (and PATCH)
`PUT` (update) and `PATCH` are used to update a resource. While similar, there
are differences and you should use the one that best suits your use case. Read
more about HTTP methods on
[MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods).
The short version of it: `PUT` requires the entire object to be submitted, while
`PATCH` requires only the properties that are different to be submitted.
An example of an update endpoint using `PUT`:
```tsx routes/api/users/[id].ts
export const handler: Handlers<User | null> = {
async PUT(req, ctx) {
const id = ctx.params.id;
const user = (await req.json()) as User;
const userKey = ["user", id];
const userRes = await kv.get(userKey);
if (!userRes.value) return new Response(`no user with id ${id} found`);
const ok = await kv.atomic().check(userRes).set(userKey, user).commit();
if (!ok) throw new Error("Something went wrong.");
return new Response(JSON.stringify(user));
},
};
```
Time to change their name. We'll now `PUT` a request to
`http://localhost:8000/api/users/2` like:
```json Request body
{
"id": "2",
"name": "New Name"
}
```
We should receive:
```json Response body
{ "id": "2", "name": "New Name" }
```
If, on the other hand, we chose to implement this as a `PATCH` operation, the
request would just involve the changed property like this:
```json Response body
{
"name": "New Name"
}
```
No need to send in the id in this case.
## DELETE
`DELETE` (delete) is used to delete a resource.
```tsx routes/api/users/[id].ts
export const handler: Handlers<User | null> = {
async DELETE(_req, ctx) {
const id = ctx.params.id;
const userKey = ["user", id];
const userRes = await kv.get(userKey);
if (!userRes.value) return new Response(`no user with id ${id} found`);
const ok = await kv.atomic().check(userRes).delete(userKey).commit();
if (!ok) throw new Error("Something went wrong.");
return new Response(`user ${id} deleted`);
},
};
```
Try sending `DELETE` to `http://localhost:8000/api/users/2` without a body.
We'll get back:
```txt Response body
user 2 deleted
```
## OPTIONS
Options can be used for some advanced cases, including implementing preflight
request checks for complex CORS use cases. See more on the
[CORS documentation](/docs/examples/dealing-with-cors).
## Full File Reference
<details>
<summary><code>[id].ts</code></summary>
```ts routes/api/users/[id].ts
import { Handlers } from "$fresh/server.ts";
type User = {
id: string;
name: string;
};
const kv = await Deno.openKv();
export const handler: Handlers<User | null> = {
async GET(_req, ctx) {
const id = ctx.params.id;
const key = ["user", id];
const user = (await kv.get<User>(key)).value!;
return new Response(JSON.stringify(user));
},
async DELETE(_req, ctx) {
const id = ctx.params.id;
const userKey = ["user", id];
const userRes = await kv.get(userKey);
if (!userRes.value) return new Response(`no user with id ${id} found`);
const ok = await kv.atomic().check(userRes).delete(userKey).commit();
if (!ok) throw new Error("Something went wrong.");
return new Response(`user ${id} deleted`);
},
async PUT(req, ctx) {
const id = ctx.params.id;
const user = (await req.json()) as User;
const userKey = ["user", id];
const userRes = await kv.get(userKey);
if (!userRes.value) return new Response(`no user with id ${id} found`);
const ok = await kv.atomic().check(userRes).set(userKey, user).commit();
if (!ok) throw new Error("Something went wrong.");
return new Response(JSON.stringify(user));
},
};
```
</details>
<details>
<summary><code>index.ts</code></summary>
```ts routes/api/users/index.ts
import { Handlers } from "$fresh/server.ts";
type User = {
id: string;
name: string;
};
const kv = await Deno.openKv();
export const handler: Handlers<User | null> = {
async GET(_req, _ctx) {
const users = [];
for await (const res of kv.list({ prefix: ["user"] })) {
users.push(res.value);
}
return new Response(JSON.stringify(users));
},
async POST(req, _ctx) {
const user = (await req.json()) as User;
const userKey = ["user", user.id];
const ok = await kv.atomic().set(userKey, user).commit();
if (!ok) throw new Error("Something went wrong.");
return new Response(JSON.stringify(user));
},
};
```
</details>