What Are API Cookies? Definition and Security
API cookies carry session state in Set-Cookie and Cookie headers. Learn SameSite, HttpOnly, Secure, CSRF and how to handle them in API tests.
What are API cookies?
API cookies are small name-value pairs that an API server sets with a Set-Cookie response header and that the client sends back in a Cookie request header on later calls to the same host. Most often the cookie holds an opaque session identifier, so a stateless HTTP server can recognise a logged-in caller without asking for credentials on every request.
Cookies matter whenever you call or test an authenticated API. If a client, monitor or load-test script drops the cookie after login, every protected call fails with a 401 even though the API is healthy.
How cookies work with APIs
The exchange is a round trip over two headers. A login response sets the cookie:
HTTP/1.1 200 OK
Set-Cookie: sessionId=abc123; Path=/; HttpOnly; Secure; SameSite=Lax
The client stores it and replays it on every matching request. Several cookies travel in one header, separated by a semicolon and a space:
GET /api/v1/orders HTTP/1.1
Host: api.example.com
Cookie: sessionId=abc123; theme=dark
The server looks up abc123 in its session store, finds the user and authorises the call. Per the MDN Set-Cookie reference, a cookie with neither Expires nor Max-Age is a session cookie that lasts until the client shuts down. If both are set, Max-Age wins. The current RFC 6265bis draft recommends that browsers cap the lifetime at 400 days.
Cookie attributes that matter
| Attribute | What it does |
|---|---|
HttpOnly | Hides the cookie from JavaScript (document.cookie). The browser still sends it with fetch() calls, so it limits what an XSS bug can steal. |
Secure | Sends the cookie only over HTTPS (localhost is exempt). An http: page cannot set a Secure cookie. |
SameSite | Strict, Lax or None: whether the cookie is sent on cross-site requests. None requires Secure. |
Domain | If omitted, the cookie goes only to the host that set it. Setting it also includes all subdomains. |
Path | Limits the cookie to URLs under a path prefix. It is not a security boundary. |
Max-Age / Expires | When the cookie is deleted. Neither set means a session cookie. |
SameSite=Strict withholds the cookie on any cross-site request. Lax sends it on top-level navigations that use a safe method such as GET, but not on background cross-site calls. Some browsers apply Lax when the attribute is missing, so state it explicitly instead of relying on that. Cookie name prefixes tighten things further: a __Host- cookie must be Secure, have Path=/ and carry no Domain.
Cookie auth vs bearer tokens
APIs usually authenticate in one of two styles. Cookie (session) auth keeps a session id in a cookie that the browser attaches by itself. Token auth, such as an bearer token or a JWT, is sent explicitly in an Authorization header.
| Aspect | Cookie auth | Bearer token |
|---|---|---|
| Sent by | Browser, automatically | Your code, explicitly |
| Server state | Session store | Often none (self-contained token) |
| CSRF risk | Yes, needs SameSite and a CSRF token | Low, nothing is attached automatically |
| XSS risk | Lower with HttpOnly | Higher if stored where scripts can read it |
| Best fit | Browser apps on one domain | Mobile apps, third-party clients, service-to-service |
Neither is strictly better, and many systems use both. A long-lived API key is a third option for server-to-server access. For the wider picture of attacks and controls, see API security.
CSRF: the cost of automatic cookies
Because the browser attaches cookies by itself, a malicious page can make the victim's browser send a request to your API with the session cookie included. That is cross-site request forgery (CSRF). Defences stack: SameSite=Lax or Strict, a CSRF token that the client must echo in a header or body field, and checking the Origin header on state-changing requests. Bearer-token APIs largely avoid CSRF, but then the token must be stored safely against XSS.
Cookies in API monitoring and load testing
A tool that calls an authenticated API must behave like a browser: capture Set-Cookie from the login response, store it and send the matching Cookie header afterwards. Most HTTP libraries offer a cookie jar, and curl has --cookie-jar and --cookie.
In a load test, each virtual user needs its own cookie jar, filled by its own login, so sessions do not collide. JMeter's HTTP Cookie Manager keeps a separate cookie store for each thread, and LoadFocus runs your existing JMX files in the cloud, so that setup carries over. For token-based APIs, LoadFocus API monitoring can log in, pass the returned token to the next request and check every step. Page-speed audits of logged-in pages can send a session cookie as a custom header with page speed monitoring. More on authenticated test flows: OAuth2 load testing and realistic e-commerce load scenarios. For the basics of API checks, read API testing.
Common mistakes
- Dropping the cookie after login in a script or monitor, then chasing a phantom outage of 401 responses.
- Sharing one cookie jar across all virtual users, so thousands of simulated users appear as one session.
- Setting
SameSite=NonewithoutSecure: browsers reject the cookie. - Testing a
Securecookie over plain HTTP, where it is never sent (localhost aside). - Putting user data in the cookie instead of an opaque id, or keeping sessions alive for weeks.
- Relying on cookies alone for state-changing requests, with no CSRF token.
FAQ about API cookies
What is the difference between a cookie and a token in an API?
A cookie is attached by the browser automatically and usually points to a server-side session. A bearer token is sent explicitly in an Authorization header and is often self-contained. Cookies suit first-party web apps, tokens suit mobile apps and service clients.
Are cookies secure for API authentication?
They can be, when configured correctly. Use HttpOnly to block script access, Secure to force HTTPS, SameSite plus a CSRF token against forged requests, and a short lifetime. A cookie without these settings is a real risk.
What does the SameSite attribute do?
It controls whether a cookie is sent on cross-site requests. Strict never sends it cross-site, Lax sends it only on top-level navigations with a safe method, and None sends it everywhere but requires Secure.
Why does my API monitor or load test get a 401 after a successful login?
Usually the tool is not keeping the session cookie. Capture Set-Cookie from the login response and send it on every protected call, with one cookie jar per virtual user.
Can a REST API use cookies?
Yes, but a server-side session behind a cookie relaxes REST's statelessness. Many teams use cookies for the browser front end and bearer tokens for programmatic access.
Related terms
- API Performance Metrics: Latency, Throughput, Error Rate
- What are Internal APIs?
- Top API Security Risks
- What is a Payload in an API? JSON, XML, Examples
- What is an API Endpoint? Definition, Examples, Best Practices
- What is an API Key? Definition, Use and Security
- What is an API Security Audit?
- What is API Abuse?
Related LoadFocus Tools
Put this concept into practice with LoadFocus, the same platform that powers everything you just read about.