Basic Auth

A guide on how to use Basic Auth for authenticating our REST API.

Overview

Basic Auth is supported in most request-libraries and is often as simple as adding a username- and password parameter. To get you up and running quickly, we here present a few language-specific methods by fetching a list of projects available from the REST API using a Service Account for access control.

Prerequisites

  • Service Account Credentials A Service Account must be created with a membership in the project(s) you intend to use it in. Any role will suffice

Code Sample

Add the following environment variables as they will be used to authenticate the API. Replace the values of the form <VARIABLE> with your own details.

export DT_SERVICE_ACCOUNT_KEY_ID=<YOUR_SERVICE_ACCOUNT_KEY_ID>
export DT_SERVICE_ACCOUNT_SECRET=<YOUR_SERVICE_ACCOUNT_SECRET>

While the following snippet sends a GET request to the API for which a list of all available projects is returned, by replacing the request URL, all the available calls in our REST API Reference can be used.

Python 3.9
Node.js 14
Go 1.16
cURL
Python 3.9
import os
import requests # pip install requests==2.25.1
if __name__ == '__main__':
# Send GET request to endpoint of choice with Basic Auth authentication.
device_listing = requests.get(
url='https://api.d21s.com/v2/projects',
auth=(
os.environ.get('DT_SERVICE_ACCOUNT_KEY_ID'),
os.environ.get('DT_SERVICE_ACCOUNT_SECRET'),
)
)
# Print response contents.
print(device_listing.json())
Node.js 14
// modules
const axios = require('axios').default; // npm install [email protected]
async function main() {
// Send GET request to endpoint of choice with Basic Auth authentication.
const response = await axios({
method: 'GET',
url: 'https://api.disruptive-technologies.com/v2/projects',
auth: {
username: process.env.DT_SERVICE_ACCOUNT_KEY_ID,
password: process.env.DT_SERVICE_ACCOUNT_SECRET,
}
})
// Print response contents.
console.log(JSON.stringify(response.data, null, 2))
}
main()
Go 1.16
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
// Construct endpoint URL for listing devices in project.
deviceListUrl := "https://api.d21s.com/v2/projects"
// Create a custom http Client with timeout.
client := &http.Client{Timeout: time.Second * 3}
// Create the request object with method, URL, but no optional body.
req, err := http.NewRequest("GET", deviceListUrl, nil)
if err != nil {
log.Fatal(err)
}
// Set the request's Authorization header to use HTTP Basic Authentication.
req.SetBasicAuth(
os.Getenv("DT_SERVICE_ACCOUNT_KEY_ID"),
os.Getenv("DT_SERVICE_ACCOUNT_SECRET"),
)
// Send an HTTP request and return an HTTP response.
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// Convert response body to map.
var data map[string]interface{}
json.NewDecoder(response.Body).Decode(&data)
// Print response body.
fmt.Println(data)
}
cURL
curl -X GET "https://api.disruptive-technologies.com/v2/projects" \
-H "accept: application/json" \
-u $DT_SERVICE_ACCOUNT_KEY_ID:$DT_SERVICE_ACCOUNT_SECRET