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

AttributeWhat it does
HttpOnlyHides the cookie from JavaScript (document.cookie). The browser still sends it with fetch() calls, so it limits what an XSS bug can steal.
SecureSends the cookie only over HTTPS (localhost is exempt). An http: page cannot set a Secure cookie.
SameSiteStrict, Lax or None: whether the cookie is sent on cross-site requests. None requires Secure.
DomainIf omitted, the cookie goes only to the host that set it. Setting it also includes all subdomains.
PathLimits the cookie to URLs under a path prefix. It is not a security boundary.
Max-Age / ExpiresWhen 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.

AspectCookie authBearer token
Sent byBrowser, automaticallyYour code, explicitly
Server stateSession storeOften none (self-contained token)
CSRF riskYes, needs SameSite and a CSRF tokenLow, nothing is attached automatically
XSS riskLower with HttpOnlyHigher if stored where scripts can read it
Best fitBrowser apps on one domainMobile 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=None without Secure: browsers reject the cookie.
  • Testing a Secure cookie 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.

How fast is your website?

Elevate its speed and SEO seamlessly with our Free Speed Test.

Free Website Speed Test

Analyze your website's load speed and improve its performance with our free page speed checker.