TUTORIAL

Basic Auth

What is Basic Auth about?

When talking to a service over the internet it is sometimes needed to let the service know who you are. That is why you sometimes need to identify yourself with a username and a password. HTTP Basic Auth is one way to do this.

How to use Basic Auth.

First: We need a service to communicate with and a username and a password.

We’ll use the 46elks API, and to get a username and a password you create an account. Inside the dashboard you’ll find your “API Username” and “API password”.

Your api username and api password that you find in the dashboard credentials should be used for every API request using Basic Authentication. That way 46elks knows who you are and what you are allowed to do (like sending an SMS from your phone number).

One way to do this is to include username and password in the URL when you are sending an API request:

https://YOUR-USERNAME:YOUR-PASSWORD@api.46elks.com/a1/…

Step by step

First try visiting this URL in your browser https://api.46elks.com/a1 This is also what is usually called the base url of an api.

You’ll see that the browser asks you for a username and a password. For now click cancel.

Let’s say your API username is USERNAME And your API password is PASSWORD

Now replace the USERNAME and PASSWORD below and with your username and your password

And then open this link https://USERNAME:PASSWORD@api.46elks.com/a1

Ta da! You’ve now used basic auth to authenticate yourself when making a request to an api. If you visit this link you’ll see that 46elks know who you are based on your username and password:

https://USERNAME:PASSWORD@api.46elks.com/a1/me

Authorization HTTP Header

Including your username and password in the URL is one of the easiest ways to use Basic Auth, but a more common way is to set username and password in the Authorization HTTP header. This is done by Base64 encoding username:password and setting the Authorization HTTP Header to Basic theBase64EncodedString.

For example the username 46elks with password secretPassword would mean that the string 46elks:secretPassword needs to be Base64 encoded which results in NDZlbGtzOnNlY3JldFBhc3N3b3Jk. In each request to the 46elks API you would then need to set the Authorization HTTP Header to the value:

Basic NDZlbGtzOnNlY3JldFBhc3N3b3Jk

Most HTTP libraries in most programming languages handle this for you. So all you need to do is specify username, password and that Basic Auth is used and the library will take care of the rest.

Get started

To start using Basic Auth in your programming langugage you can take a look at the many code examples on github and read the tutorials on how to talk to the 46elks API. Take a look at the documentation to see how to make a request to the api to send an SMS, or trigger a phone call.