docs / articles / API Basic Authentication Example

API Basic Authentication Example

Sara Jul 09, 2026 2026-07-09 04:40:45

APIs, or Application Programming Interfaces, are the backbone of modern web applications, enabling seamless communication between different software systems. Among the various types of API authentication methods, Basic Authentication is one of the simplest and most commonly used. Let's delve into an example of API Basic Authentication to understand its implementation and usage.

Ever needed to expose an endpoint to an external system but didn’t want to set up a full identity system? | Milan Jovanović
Ever needed to expose an endpoint to an external system but didn’t want to set up a full identity system? | Milan Jovanović

Basic Authentication, as the name suggests, is a basic form of API authentication. It involves sending a username and password encoded in Base64 format with every API request. This method is easy to implement but is not the most secure due to the plaintext transmission of credentials.

🔍Check out this Flow Diagram on REST API Authentication Methods!🔐
🔍Check out this Flow Diagram on REST API Authentication Methods!🔐

Understanding Basic Authentication

Before we dive into an example, let's understand the basics of Basic Authentication. In this method, the client sends a request to the server with the following header:

Big List of Free and Open Public APIs (No Auth Needed) [2026]
Big List of Free and Open Public APIs (No Auth Needed) [2026]

Authorization: Basic

Encoding the Username and Password

the back cover of an application with instructions for testing and troublesing it, including text
the back cover of an application with instructions for testing and troublesing it, including text

The username and password are concatenated with a colon (:) and then encoded in Base64 format. For instance, if your username is 'admin' and your password is 'secret', the encoded string would be:

admin:secret

After encoding in Base64, the string becomes:

Basic Authentication using Web API with example | ASP.NET [Latest Tutorial]
Basic Authentication using Web API with example | ASP.NET [Latest Tutorial]

YWRtaW46c2VjcmV0

Sending the Request

This encoded string is then sent in the Authorization header of the API request. Here's an example of a GET request using Basic Authentication:

the different types of aitems and their uses in business infographical poster
the different types of aitems and their uses in business infographical poster

GET /api/data HTTP/1.1 Host: api.example.com Authorization: Basic YWRtaW46c2VjcmV0

Implementing Basic Authentication in an API

What is an API? Full Form, Meaning, Definition, Types & Example
What is an API? Full Form, Meaning, Definition, Types & Example
Implementing API Key Authentication in ASP.NET Core
Implementing API Key Authentication in ASP.NET Core
The API key that Google had announced as 'OK to publish' is also the Gemini authentication key, so there are a lot of websites that are leaking personal information
The API key that Google had announced as 'OK to publish' is also the Gemini authentication key, so there are a lot of websites that are leaking personal information
What's an API?
What's an API?
API Documentation Graphic for Tech Guides
API Documentation Graphic for Tech Guides
API Documentation: What is it & How To Create them? - Blog
API Documentation: What is it & How To Create them? - Blog
What are API Calls?
What are API Calls?
Webflow CMS API overview: How to get your API key and integrate it.
Webflow CMS API overview: How to get your API key and integrate it.
What is an API? Simple Explanation for Non-Developers
What is an API? Simple Explanation for Non-Developers
Using Auth0 to Secure Your Angular Application and Access Your ASP.NET Core API
Using Auth0 to Secure Your Angular Application and Access Your ASP.NET Core API
REST API Testing Tutorial – Sample Manual Test Cases
REST API Testing Tutorial – Sample Manual Test Cases
7 Free APIs for Your Next Projects
7 Free APIs for Your Next Projects
VB .NET Google Drive Api Source Code Example
VB .NET Google Drive Api Source Code Example
Pinterest: “API method not found” [Updated]
Pinterest: “API method not found” [Updated]
AWS API Gateway with Custom Authentation
AWS API Gateway with Custom Authentation
GitHub - public-apis/public-apis: A collective list of free APIs
GitHub - public-apis/public-apis: A collective list of free APIs
How to Perform API Intercept Using Playwright
How to Perform API Intercept Using Playwright
API Reference
API Reference
Laravel 5 REST Api Basic Authentication Tutorial
Laravel 5 REST Api Basic Authentication Tutorial

Now that we understand the basics let's see how to implement Basic Authentication in an API. We'll use Node.js with the Express framework for this example.

First, install the necessary packages:

npm install express basic-auth

Setting Up the Server

Create an Express server and set up a route that requires authentication:

const express = require('express'); const basicAuth = require('basic-auth'); const app = express(); const users = { admin: 'secret' }; app.get('/api/data', (req, res) => { const user = basicAuth(req); if (!user || user.name !== 'admin' || user.pass !== 'secret') { res.statusCode = 401; res.setHeader('WWW-Authenticate', 'Basic realm="Example"'); res.end(); return; } res.json({ message: 'Access granted.' }); }); app.listen(3000, () => { console.log('Server listening on port 3000'); });

Testing the API

Start the server and test the API using a tool like curl or Postman. Here's an example using curl:

curl -X GET -u admin:secret http://localhost:3000/api/data

If the credentials are correct, you should receive a JSON response with the message 'Access granted.'

While Basic Authentication is simple to implement, it's essential to remember its limitations. Due to the plaintext transmission of credentials, it's not suitable for use over unencrypted connections. For more secure communication, consider using methods like OAuth or API keys.