Skip to content

Basic Authentication with samples

As of Adaptix 2.3.0, support for basic authentication can be enabled through Adaptix's Configuration -> API Settings. As with OAuth2, it is only recommended to use basic authentication over HTTPS.

To authorize a request for basic authentication, set an Authorization header.

  • Combine the username and password of a Adaptix user with a colon :. For example, user:password.
  • Base64 encode the string from above. dXNlcjpwYXNzd29yZA==.
  • Add an Authorization header to each API request as Authorization: Basic dXNlcjpwYXNzd29yZA==
  • The URL should be a known endopoint i.e. api/contacts

CURL example

curl -H "Authorization: Basic $(echo -n 'username:password' | base64)" https://instancename.adaptix.ai/api/contacts

PHP example

$settings will be an array like this:

1
2
3
4
5
6
<?php
// ApiAuth->newAuth() will accept an array of Auth settings
$settings = [
    'userName'   => '',             // Create a new user       
    'password'   => '',             // Make it a secure password
];

Also, for basic auth, pass 'BasicAuth' in 2nd parameter:

1
2
3
// Initiate the auth object
$initAuth = new ApiAuth();
$auth     = $initAuth->newAuth($settings, 'BasicAuth');

Java example

Make a GET request with Basic Authentication to the endpoint https://instancename.adaptix.ai/api/contacts using HttpURLConnection:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class AdaptixApiClient {

    public static void main(String[] args) {
        try {
            String url = "https://instancename.adaptix.ai/api/contacts";
            String username = "username";
            String password = "password";

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // Set method to GET
            con.setRequestMethod("GET");

            // Basic Auth header
            String auth = username + ":" + password;
            String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
            String authHeaderValue = "Basic " + encodedAuth;
            con.setRequestProperty("Authorization", authHeaderValue);

            int responseCode = con.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // Read response
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // Print the response
            System.out.println("Response Body:");
            System.out.println(response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

JavaScript example

Using fetch() to make a GET request with Basic Authentication to:

const username = 'username';
const password = 'password';
const url = 'https://instancename.adaptix.ai/api/contacts';

const headers = new Headers();
headers.set('Authorization', 'Basic ' + btoa(username + ':' + password));

fetch(url, {
    method: 'GET',
    headers: headers
})
.then(response => {
    if (!response.ok) {
        throw new Error('HTTP error ' + response.status);
    }
    return response.json();
})
.then(data => {
    console.log('Response:', data);
})
.catch(error => {
    console.error('Error:', error);
});
  • btoa() encodes the username and password into a base64 string as required by Basic Auth.

  • Always use HTTPS when sending credentials.

  • If you're working in Node.js, you can use node-fetch or Axios with similar logic.

Python example

Using the requests library to make a GET request with Basic Authentication to:

import requests
from requests.auth import HTTPBasicAuth

url = "https://instancename.adaptix.ai/api/contacts"
username = "username"
password = "password"

response = requests.get(url, auth=HTTPBasicAuth(username, password))

if response.status_code == 200:
    print("Response JSON:")
    print(response.json())
else:
    print(f"Failed with status code {response.status_code}")
    print(response.text)

Make sure requests is installed:

pip install requests
Replace instancename with your actual subdomain.

All the above example returns the Contacts list in JSON like the following,

{
    "total": "1",
    "contacts": {
        "47": {
            "id": 47,
            "isPublished": true,
            "dateAdded": "2015-07-21T12:27:12-05:00",
            "createdBy": 1,
            "createdByUser": "Joe Smith",
            "dateModified": "2015-07-21T14:12:03-05:00",
            "modifiedBy": 1,
            "modifiedByUser": "Joe Smith",
            "owner": {
                "id": 1,
                "username": "joesmith",
                "firstName": "Joe",
                "lastName": "Smith"
            },
            "points": 10,
            "lastActive": "2015-07-21T14:19:37-05:00",
            "dateIdentified": "2015-07-21T12:27:12-05:00",
            "color": "ab5959",
            "ipAddresses": {
                "111.111.111.111": {
                    "ipAddress": "111.111.111.111",
                    "ipDetails": {
                        "city": "",
                        "region": "",
                        "country": "",
                        "latitude": "",
                        "longitude": "",
                        "isp": "",
                        "organization": "",
                        "timezone": ""
                    }
                }
            },
            "fields": {
                "core": {
                    "title": {
                        "id": "1",
                        "label": "Title",
                        "alias": "title",
                        "type": "lookup",
                        "group": "core",
                        "value": "Mr"
                    },
                    "firstname": {
                        "id": "2",
                        "label": "First Name",
                        "alias": "firstname",
                        "type": "text",
                        "group": "core",
                        "value": "Jim"
                    },

                    "...": {
                        "..." : "..."
                    }
                },
                "social": {
                    "twitter": {
                        "id": "17",
                        "label": "Twitter",
                        "alias": "twitter",
                        "type": "text",
                        "group": "social",
                        "value": "jimcontact"
                    },

                    "...": {
                        "..." : "..."
                    }
                },
                "personal": [],
                "professional": [],
                "all": {
                    "title": "Mr",
                    "firstname": "Jim",
                    "twitter": "jimcontact",

                    "...": "..."
                }
            },
            "tags": [{
              "tag": "aTag"
            },
            {
              "tag": "bTag"
            }],
            "utmtags" : [{
              "id": 1,
              "query": {
                "page": "asd",
                "cid": "fb1"
              },
              "referer": "https://example.com/",
              "remoteHost": "example.com",
              "userAgent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0",
              "utmCampaign": "abcampaign",
              "utmContent": "page",
              "utmMedium": "social",
              "utmSource": "fb",
              "utmTerm": "test1"
            }],
            "doNotContact": [{
                "id": 2,
                "reason": 2,
                "comments": "",
                "channel": "email",
                "channelId": null
            }]
        }
    }
}