skills/bun-runtime-http-cookies/SKILL.md
Work with cookies in HTTP requests and responses using Bun's built-in Cookie API.
npx skillsauth add jarle/bun-skills Bun CookiesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Work with cookies in HTTP requests and responses using Bun's built-in Cookie API.
Bun provides a built-in API for working with cookies in HTTP requests and responses. The BunRequest object includes a cookies property that provides a CookieMap for easily accessing and manipulating cookies. When using routes, Bun.serve() automatically tracks request.cookies.set and applies them to the response.
Read cookies from incoming requests using the cookies property on the BunRequest object:
Bun.serve({
routes: {
"/profile": req => {
// Access cookies from the request
const userId = req.cookies.get("user_id");
const theme = req.cookies.get("theme") || "light";
return Response.json({
userId,
theme,
message: "Profile page",
});
},
},
});
To set cookies, use the set method on the CookieMap from the BunRequest object.
Bun.serve({
routes: {
"/login": req => {
const cookies = req.cookies;
// Set a cookie with various options
cookies.set("user_id", "12345", {
maxAge: 60 * 60 * 24 * 7, // 1 week
httpOnly: true,
secure: true,
path: "/",
});
// Add a theme preference cookie
cookies.set("theme", "dark");
// Modified cookies from the request are automatically applied to the response
return new Response("Login successful");
},
},
});
Bun.serve() automatically tracks modified cookies from the request and applies them to the response.
To delete a cookie, use the delete method on the request.cookies (CookieMap) object:
Bun.serve({
routes: {
"/logout": req => {
// Delete the user_id cookie
req.cookies.delete("user_id", {
path: "/",
});
return new Response("Logged out successfully");
},
},
});
Deleted cookies become a Set-Cookie header on the response with the maxAge set to 0 and an empty value.
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling