Base64 Encode & Decode · Tutorial
How to create a Basic Auth header with Base64
5 min readbeginnerUpdated
Many APIs, webhooks and internal dashboards use HTTP Basic authentication, which expects your username and password joined by a colon and Base64-encoded in an Authorization header. This tutorial walks through building that header correctly, including the details that most often cause a 401 Unauthorized response.
What you need
- The Base64 Encode & Decode
- The username and password (or API key and secret) for the service
- Optionally, a terminal with
curlto test the header
Steps
Join the username and password with a colon
Type the two values into the left box as
username:password, with no spaces around the colon. Some services give you an API key and secret instead: use the key as the username and the secret as the password. The sample loaded by Try it isapi_user:s3cret-Passw0rd.Keep the standard Base64 alphabet
Leave URL-safe and Wrap lines unticked. The HTTP standard for Basic auth uses regular Base64 with
+,/and=padding, on a single line. The encoded value appears on the right straight away.YXBpX3VzZXI6czNjcmV0LVBhc3N3MHJkAdd the Basic prefix
The header value is the word
Basic, one space, then the encoded string. Press Copy and put it together like this.Authorization: Basic YXBpX3VzZXI6czNjcmV0LVBhc3N3MHJkTest the header
Send a request with the header to confirm the credentials work. A
200response means success. A401means the server rejected the credentials or the header format.curl -H "Authorization: Basic YXBpX3VzZXI6czNjcmV0LVBhc3N3MHJk" \ https://api.example.com/v1/statusDecode it to double-check
If something is wrong, paste the encoded part (without
Basic) into Base64 Decode to see exactly what the server receives. Stray spaces or a missing character show up immediately.
Try it yourself
Opens the Base64 Encode & Decode with this tutorial's sample loaded.
Common mistakes
- A trailing space or newline
- Copying credentials from a document often brings a hidden space or line break along, which becomes part of the password. Make sure the left box contains exactly
username:passwordand nothing else. - Using the URL-safe variant
- URL-safe Base64 replaces
+and/and drops=. Servers expect the standard form, so a password that happens to produce those characters will fail with URL-safe output. - Treating Base64 as protection
- Anyone who sees the header can decode the password. Only send Basic auth over HTTPS, and never commit the encoded header to a public repository.
Next steps
Go back to the Base64 Encode & Decode, or keep going with one of these:
FAQ
Does curl have a shortcut for this?
Yes. curl -u username:password URL builds the same header for you. Encoding it yourself is useful for tools, config files and API clients that ask for the raw header value.
What if my password contains a colon?
That is allowed. Servers split on the first colon only, so the username itself must not contain one.