docs/ts/primitives/cookies.mdx
Encore.ts provides type-safe cookie handling for your API endpoints. Cookies are commonly used for session management, personalization, and tracking. This guide explains how to use cookies in different contexts within your Encore.ts application.
Cookies can be utilized in three main contexts within Encore.ts:
Cookies can be used in authentication handlers:
import { Cookie, Gateway } from "encore.dev/api";
import { authHandler } from "encore.dev/auth";
// Define auth parameters with cookies
interface AuthParams {
sessionId: Cookie<"sessionId">;
}
// Auth handler that uses cookies
const auth = authHandler<AuthParams, User>(async ({ sessionId }) => {
return validateAndGetUser(sessionId.value);
});
// Configure the gateway with the auth handler
export const gateway = new Gateway({
authHandler: auth,
});
You can set cookies in your API responses by including them in your response type:
import { api, Cookie } from "encore.dev/api";
// Define a response type with a cookie
interface LoginResponse {
user: UserData;
sessionId: Cookie<"sessionId">;
}
// Create an API endpoint that sets a cookie
export const login = api<LoginParams, LoginResponse>({
method: "POST",
path: "/login",
expose: true,
}, async (params): Promise<LoginResponse> => {
// Authenticate user
const user = await authenticateUser(params.username, params.password);
const sessionId = generateSessionId();
// Store session
await storeSession(sessionId, user.id);
// Return response with cookie
return {
user,
sessionId: {
value: sessionId,
httpOnly: true,
secure: true,
sameSite: "Strict",
maxAge: 60 * 60 * 24 * 7, // 7 days
}
};
});
When creating API endpoints, you can access cookies sent by the client by defining them in your parameter type:
import { api } from "encore.dev/api";
// Define a request type with a cookie
interface Params {
language?: Cookie<"language">;
}
// Create an API endpoint that uses the cookie
export const get = api<Params, { msg: string }>(
{
method: "GET",
path: "/user/profile",
expose: true,
},
async ({ language }) => {
// Access the cookie value
const lang = language?.value ?? "en";
return { msg: `your language: ${lang}` };
},
);
Encore.ts allows you to specify the type of cookie values for improved type safety. By default, cookie values are strings, but you can define cookies with different data types. If you omit the type parameter, Encore.ts will automatically use string as the type.
You can use the following types for cookie values:
string: Text data (default when generic parameter is omitted)number: Numeric valuesboolean: True/false valuesDate: Date objectsWhen defining cookies, you can specify the type using the generic parameter:
interface UserPreferences {
// Cookies with different types
userId: Cookie<number, "userId">;
darkMode: Cookie<boolean, "darkMode">;
lastVisit: Cookie<Date, "lastVisit">;
language: Cookie<string, "language">;
// Using omitted type parameter (implicitly string)
theme: Cookie<"theme">;
}
export const savePreferences = api<UserPreferences, void>({
method: "POST",
path: "/user/preferences",
expose: true,
}, async (params) => {
// Type-safe access to cookie values
const userId = params.userId.value; // number
const isDarkMode = params.darkMode.value; // boolean
const lastVisit = params.lastVisit.value; // Date
const language = params.language.value; // string
const theme = params.theme.value; // string (implicitly typed)
// Save preferences...
});
When setting cookies, you can configure various options:
"Strict": Only sent in same-site requests"Lax": Sent with same-site requests and top-level navigations"None": Sent with all requests (requires secure: true)The generated Encore.ts client does not explicitly handle cookies. Instead, it relies on the browser's built-in cookie handling. When using the client in browser environments, cookies will be automatically included in requests and stored from responses.
For cross-site requests, you need to configure the client to include credentials:
// Create a client that includes credentials (cookies) for cross-site requests
const client = new Client(Local, { requestInit: { credentials: "include" } });
httpOnly: true for cookies containing sensitive datasecure: true for production environmentssameSite settings based on your requirementsmaxAge or expires options to limit cookie lifetimeBy following these guidelines, you can effectively leverage cookies in your Encore.ts applications while maintaining security and type safety.