ProxyWing LogoProxyWing

cURL Authentication Guide: From Basic to Advanced Methods

Testing endpoints, scraping protected sites, and connecting services with API credentials saves hours of debugging and keeps your data safe. You can send cookies or tokens from the command line using cURL. The cURL with authorization header and many authentication methods, from basic to advanced, are covered in this article. HTTP requests, curl commands, and API requests are all handled by cURL clients.

Published:October 21, 2025
Reading time:7 min
Last updated:June 17, 2026

Understanding Authentication in cURL

Web requests must first go through authentication before accessing protected data or operations. The web site and API are only accessible to approved users or clients. cURL supports several different authentication methods, including Basic Auth, Bearer token, and cookies. There are many ways to send credentials, such as encoding, cookie storage, and authorization headers. For example,

curl -u username:password https://api.example.com/data

Using the cURL authorization header, this small script will transmit authentication data. It examines requests for web apps and services and who can access them.

cURL supports a few distinct authentication methods, and the right one depends on what the server expects. The table below summarizes when to use each.

MethodcURL flag / headerTypical use case
Basic Auth-u user:pass or -H "Authorization: Basic ..."Simple APIs, internal tools, quick tests
Bearer token-H "Authorization: Bearer <token>"REST APIs, OAuth-protected endpoints
API key-H "X-API-Key: <key>" or query parameterPublic APIs that issue static keys
Cookie / session-c and -bSites that log you in and track a session
OAuth 2.0token request, then BearerDelegated access, third-party APIs

The rest of this guide covers each method with copy-paste examples.

Basic Authentication With cURL

Sending cURL login credentials using basic authentication is the simplest method. The password is sent with -u.

curl -u user:password https://api.example.com/profile

CURL transmits BA credentials using Base64 encoding. dXNlcjpwYXNzd29yZA==. Easy technique to give up credentials using connections that aren’t secure. HTTPS send data, including encrypted passwords. You can specify whether to utilize automated or manual endpoint authentication on various web servers. This is what API clients use for quick integrations that aren’t for production.

Sending Basic Auth as a Manual Header

The -u flag is the shortcut, but cURL turns it into a standard header. You can also set that header yourself:

curl -H "Authorization: Basic dXNlcjpwYXNzd29yZA==" https://api.example.com/profile

The string after Basic is just user:password encoded in Base64. dXNlcjpwYXNzd29yZA== decodes back to user:password, so it is not encryption — anyone who reads the header can decode it. That is why Basic Auth must always run over HTTPS.

To generate the encoded value yourself:

echo -n 'user:password' | base64
# dXNlcjpwYXNzd29yZA==

Prompting for the Password

To keep the password out of your shell history, pass only the username and let cURL prompt for the rest:

curl -u user https://api.example.com/profile

Storing Credentials in a .netrc File

For repeated requests, store credentials in a .netrc file instead of typing them each time:

# ~/.netrc
machine api.example.com
login user
password secret
curl --netrc https://api.example.com/profile

Set permissions with chmod 600 ~/.netrc so other users on the machine cannot read it.

Using Bearer Tokens With cURL

cURL Authentication Guide

Bearer Tokens In addition to other API authentication methods, cURL also uses bearer tokens. Instead of login and passwords, users transmit access tokens to check sessions or privileges. It’s safer to handle credentials. Including HTTP-CURL for authentication Bearer token should be in titles.

curl -H "Authorization: Bearer <your_token>" https://api.example.com/user

API interfaces and automated workflows can cancel or change tokens without providing credentials, unlike Basic Auth. Developers can access json data or json responses after authentication by using get requests. API accept headers usually tell the server what kind of data they want, such application/json. For both users and clients, using token add convenience and security. Automating curl authentication on a huge scale.

Bearer Token in GET and POST Requests

A GET request only needs the Authorization header:

curl -H "Authorization: Bearer <your_token>" https://api.example.com/user

For a POST request that sends JSON, add the token alongside the content type:

curl -X POST https://api.example.com/orders \
  -H "Authorization: Bearer <your_token>" \
  -H "Content-Type: application/json" \
  -d '{"item":"book","qty":2}'

Storing the Token in a Variable

Hard-coding tokens in commands leaks them into your shell history. Use an environment variable instead:

export TOKEN="your_token_here"
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/user

The –oauth2-bearer Shortcut

cURL has a built-in flag that sets the Bearer header for you:

curl --oauth2-bearer "your_token_here" https://api.example.com/user

Common Bearer Token Mistakes

Most 401 Unauthorized responses with a Bearer token come from one of these:

  • Missing the word Bearer before the token, or a wrong space between Bearer and the value.
  • An expired or revoked token: request a fresh one from the auth server.
  • The wrong header name (Authorization is correct, not Auth or Token).
  • Quotes dropped around the header, so the shell splits it into two arguments.

Cookie-Based Authentication

Cookie-based authentication is widespread for web pages that requires authentication you to log in. Servers also transmit session cookies with requests that come after the first one. You can save and use cookies with cURL’s -c and -b options.

curl -c cookies.txt -d "user=name&pass=secret" https://example.com/login
curl -b cookies.txt https://example.com/dashboard

Using login cookies will allow you to get session-based content. Data and dashboards are accessible to Apps requiring Authentication. Cookies are maintained in case the server needs authentication. This adds persistence for API clients who use it for a long time. Many curls web scraping and API integration methods use cookies.

Implementing Authorization Headers

When using cURL headers, you usually need to authenticate. Gives you more flexibility over how requests are structured.

curl -H "Authorization: ApiKey 12345abcde" https://api.example.com/data

Even small errors in the title name or space can result in unauthorized access. For APIs that aren’t standard, like ApiKey, CustomToken, and X-Access-Token, manual headers are helpful. For API security, many complex applications needs for manual cURL authorization header customization. Use a cUrl with header authorization request instead of curl for secure connections. Curl and dynamic data authentication allow you to transmit HTTP requests. To make things easier, change the title of your authorization. You might have to add a lot of titles to a single command using -H flags repeatedly if your API supports a lot of tokens. Give authentication to allow automation programs. Use the proper lines for setting multiple headers. Before running tokens, developers protect them by setting environment variables. Standardize automatic pipeline authentication by using configuration files that can be used again. To get secure responses after setting, use authenticated curl commands. In the sent HTTP headers, check the authorization code. For sending custom headers or testing php curl apps, you need to do advanced installs. Manual configurations add flexibility for clients that require various services.

Sending Multiple Headers

APIs often need more than one header. Repeat the -H flag for each:

curl -H "Authorization: Bearer <token>" \
     -H "X-API-Key: 12345abcde" \
     -H "Accept: application/json" \
     https://api.example.com/data

Custom Authorization Schemes

Not every API uses Bearer or Basic. Some define their own scheme name, and cURL sends whatever you put after Authorization::

curl -H "Authorization: ApiKey 12345abcde" https://api.example.com/data
curl -H "Authorization: Token 12345abcde" https://api.example.com/data

Checking Which Headers cURL Actually Sent

When a request fails, confirm what cURL sent. The -v (verbose) flag prints the request headers, including your Authorization line:

curl -v -H "Authorization: Bearer <token>" https://api.example.com/user

Lines starting with > are what cURL sent; lines starting with < are the server response. To include the response headers in the output instead, use -i:

curl -i -H "Authorization: Bearer <token>" https://api.example.com/user

Use -I (capital) to send a HEAD request and fetch only the response headers without the body.

Sending Multiple Headers

APIs often need more than one header. Repeat the -H flag for each:

curl -H "Authorization: Bearer <token>" \
     -H "X-API-Key: 12345abcde" \
     -H "Accept: application/json" \
     https://api.example.com/data

Custom Authorization Schemes

Not every API uses Bearer or Basic. Some define their own scheme name, and cURL sends whatever you put after Authorization::

curl -H "Authorization: ApiKey 12345abcde" https://api.example.com/data
curl -H "Authorization: Token 12345abcde" https://api.example.com/data

Checking Which Headers cURL Actually Sent

When a request fails, confirm what cURL sent. The -v (verbose) flag prints the request headers, including your Authorization line:

curl -v -H "Authorization: Bearer <token>" https://api.example.com/user

Lines starting with > are what cURL sent; lines starting with < are the server response. To include the response headers in the output instead, use -i:

curl -i -H "Authorization: Bearer <token>" https://api.example.com/user

Use -I (capital) to send a HEAD request and fetch only the response headers without the body.

Using API Keys and Access Tokens with cURL

Many APIs hand out a static key instead of a username and password. There are two common ways to send it.

As a header, either in a custom header or an Authorization scheme:

curl -H "X-API-Key: 12345abcde" https://api.example.com/data
curl -H "Authorization: ApiKey 12345abcde" https://api.example.com/data

As a query parameter, when the API expects the key in the URL:

curl "https://api.example.com/data?api_key=12345abcde"

Headers are the safer choice — query parameters can end up in server logs and browser history. An access token works the same way as a Bearer token: send it in the Authorization header and refresh it when it expires.

Advanced Authentication Scenarios

For some applications, tokens or passwords aren’t always adequate for advanced authentication. OAuth 2.0 needs to obtain an access token, for example, using title and data flags.
The title should state “Content-Type:

curl -X POST https://auth.example.com/token \
--header "Content-Type: application/x-www-form-urlencoded" \
--data "client_id=123&client_secret=abc&grant_type=client_credentials"

Servers reply to API requests by sending back JWTs. Some systems employ oauth tokens or API keys in query parameters, such as?api_key=XYZ, or need multiple tokens to be sent. Advanced methods improve auditing, security, and control of automated and large-scale environments. When using cURL to dynamically collect tokens, you can specify scopes or privileges during initial authentication. OAuth adds logical access control to help clients manage their API sessions. Automated and repeated authentication are advantageous for developers using curls for difficult tasks.

cURL vs OAuth: What’s the Difference?

These are not alternatives to each other. cURL is the tool that sends the request; OAuth is the framework that decides whether the request is allowed. In practice you use cURL to run the OAuth flow: first you call the token endpoint to get an access token, then you send that token as a Bearer header on every following request.

# Step 1: get an access token
curl -X POST https://auth.example.com/token \
  --header "Content-Type: application/x-www-form-urlencoded" \
  --data "client_id=123&client_secret=abc&grant_type=client_credentials"

# Step 2: use the returned token
curl -H "Authorization: Bearer <access_token>" https://api.example.com/user

cURL Authentication Best Practices

A few habits keep your credentials safe and your requests reliable.

  • Always use HTTPS. Basic Auth and tokens are readable in plain text over HTTP. HTTPS encrypts them in transit.
  • Keep secrets out of your commands. Store tokens and passwords in environment variables or a .netrc file instead of typing them inline, where they land in your shell history.
  • Avoid leaking secrets in history. Many shells skip history for commands that start with a space, and chmod 600 ~/.netrc keeps credential files private.
  • Never log tokens. Be careful with -v output in shared logs or screenshots, it prints your Authorization header.
  • Prefer short-lived tokens and rotate them. Bearer tokens that expire limit the damage if one leaks. Revoke and reissue keys you suspect are exposed.
  • Use the least access you need. When requesting OAuth tokens, ask only for the scopes the task requires.

Troubleshooting Authentication Errors

By making small errors, authentication might be compromised. Most of the time, there are 401 errors. Unauthorized, headers indicating invalid credentials or tokens. Tokens won’t work any more, requiring replacement. Missing spaces and inappropriate names for parameters make things worse. To look for problems, use -v:

curl -v -H "Authorization: Bearer <token>" https://api.example.com/data

Full request and response data rapidly shows problems with authentication. Turn on verbose error codes to get a scan. Troubleshooting adds efficiency to the work of integration developers and clients. Developers frequently make C URL logging configuration errors.

Using cURL for Web Scraping

Using API authentication, you can access content that is hidden behind login forms or private dashboards while scraping web pages or collecting data from APIs. CURL can get to protected data in the same way that a browser does, by using cookies and tokens. For examples, bearer token and cookie files scrape responses from APIs and user-specific web pages. Using these methods, you can get information such crucial from the web. This is a common use for web applications and API developers.

Platform-Specific Usage (Windows, macOS, Linux)

All platforms support cURL, although the syntax is very crucial. Windows puts double quotes (“) around titles, while macOS and Linux put single quotes around them. Windows PowerShell uses %TOKEN%, not Unix $TOKEN. There are numerous installation methods for сURL, which is present in most Linux distributions and macOS.

Wrapping Up

To sum up cURL supports all authentication methods, from basic to OAuth. Accessing APIs, web servers, and web services across platforms is faster and safer with knowledge of its authorization options, particularly the cURL header. For developers and clients who require dependable curl communication, its versatility adds value.

Article written by:

Alexandre Parfonov

Full Stack AI Engineer

Alexandre brings deep full-stack expertise to Proxywing's engineering efforts — from backend architecture and performance optimization to AI-driven development workflows. His hands-on work spans Node.js, React, cloud infrastructure, and RAG pipelines, giving him a rare ability to tackle both proxy platform internals and user-facing product challenges. At Proxywing, Alexandre focuses on designing resilient systems, eliminating performance bottlenecks, and integrating modern AI tooling into the development process. Outside of coding, he's passionate about exploring the frontiers of AI engineering and building side projects that push his technical boundaries.

All articles by author (50)

FAQ

Add an Authorization header with the `Bearer` scheme:

“`bash
curl -H “Authorization: Bearer ” https://api.example.com/user
“`

 

Use the `-u` flag with a username and password, and run it over HTTPS:

“`bash
curl -u user:password https://api.example.com/profile
“`

The part after `Basic` is `user:password` encoded in Base64. It is encoding, not encryption, so it only protects credentials when sent over HTTPS.

cURL does not create tokens. The API or auth server issues one — usually after you log in or complete an OAuth request. You then pass that token in the Authorization header.

The most common causes are an expired token, a missing or misspelled `Bearer`/`Basic` keyword, the wrong header name, or credentials sent over HTTP instead of HTTPS.

Run the request with `-v`. Lines beginning with `>` show the headers cURL sent, including your Authorization line.

Have any questions?