Course contentsShow
← JavaScript
Lesson 2272 of 2,72347. Networking PrimitivesFree lesson

Request Credentials and Authentication

Control cookie and credential behavior with the credentials option: omit, same-origin, or include.

Request Credentials and Authentication

What you'll learn: How to control whether cookies and authentication credentials are included in your fetch requests.

When you make a fetch request, you need to decide whether to send cookies, HTTP authentication headers, and TLS client certificates along with it. The credentials option controls this behavior, and choosing the right value is crucial for both security and functionality.

What Are Credentials?

Credentials include:

  • Cookies (session IDs, authentication tokens)
  • HTTP Basic/Digest authentication headers
  • TLS client certificates

Think of credentials like your wallet and ID—sometimes you want to bring them (authenticated requests), sometimes you don't need them (public APIs), and sometimes you only bring them when staying in familiar territory (same domain).

The Three Credentials Options

omit

Never send credentials, even for same-origin requests. Use this when you absolutely don't want any authentication data traveling with the request.

fetch('/api/data', { credentials: 'omit' });

same-origin (default)

Send credentials only to URLs on the same origin (same protocol, domain, and port). This is the safest default—your cookies stay home when crossing domain boundaries.

fetch('/api/user', { credentials: 'same-origin' });

include

Always send credentials, even to cross-origin URLs. Use this when you need to authenticate with third-party APIs that expect cookies.

fetch('https://api.example.com/data', { 
  credentials: 'include' 
});

Note: Cross-origin requests with credentials: 'include' require the server to explicitly allow it with CORS headers (Access-Control-Allow-Credentials: true).

Key Takeaway: The credentials option controls whether cookies and auth headers travel with your fetch requests—use omit to exclude them, same-origin for same-domain only (default), or include to always send them across origins.