Cookies API
API reference for the cookies module
Cookies API
The cookies
module provides methods to read and manipulate cookies consistently across server and client environments.
Import
Methods
cookies()
Returns the cookies object that provides access to all cookie-related operations.
get(name)
Retrieves a specific cookie by name.
Parameters
name
(string): The name of the cookie to retrieve
Returns
{ name: string; value: string } | undefined
: Cookie object if found, undefined otherwise
getAll()
Returns all available cookies.
Returns
Array<{ name: string; value: string }>
: Array of all cookie objects
set(name, value, options?)
Sets a cookie with the specified name and value.
Parameters
name
(string): Cookie namevalue
(string): Cookie valueoptions
(CookieOptions): Optional cookie configuration
delete(name)
Removes a cookie by name.
Parameters
name
(string): The name of the cookie to delete
Types
Cookie Options
maxAge
Time in seconds until the cookie expires.
expires
Specific date when the cookie should expire.
path
Path on the server for which the cookie is valid.
domain
Domain for which the cookie is valid.
secure
If true, cookie is only transmitted over secure HTTPS.
httpOnly
If true, cookie is inaccessible to JavaScript's Document.cookie API.
sameSite
Controls how the cookie behaves with cross-site requests:
- 'Strict': Only sent in first-party context
- 'Lax': Sent with navigation to origin site
- 'None': Sent in all contexts (requires secure: true)
Best Practices
- Always set appropriate security options for cookies containing sensitive data
- Use httpOnly for cookies that don't need client-side access
- Consider setting appropriate sameSite options
- Implement proper error handling for cookie operations