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 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 target project. 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.
1
export DT_SERVICE_ACCOUNT_KEY_ID=<YOUR_SERVICE_ACCOUNT_KEY_ID>
2
export DT_SERVICE_ACCOUNT_SECRET=<YOUR_SERVICE_ACCOUNT_SECRET>
Copied!
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
1
import os
2
import requests # pip install requests==2.25.1
3
4
if __name__ == '__main__':
5
# Send GET request to endpoint of choice with Basic Auth authentication.
6
device_listing = requests.get(
7
url='https://api.d21s.com/v2/projects',
8
auth=(
9
os.environ.get('DT_SERVICE_ACCOUNT_KEY_ID'),
10
os.environ.get('DT_SERVICE_ACCOUNT_SECRET'),
11
)
12
)
13
14
# Print response contents.
15
print(device_listing.json())
Copied!
1
// modules
2
const axios = require('axios').default; // npm install [email protected]
3
4
async function main() {
5
// Send GET request to endpoint of choice with Basic Auth authentication.
6
const response = await axios({
7
method: 'GET',
8
url: 'https://api.disruptive-technologies.com/v2/projects',
9
auth: {
10
username: process.env.DT_SERVICE_ACCOUNT_KEY_ID,
11
password: process.env.DT_SERVICE_ACCOUNT_SECRET,
12
}
13
})
14
15
// Print response contents.
16
console.log(JSON.stringify(response.data, null, 2))
17
}
18
main()
Copied!
1
package main
2
3
import (
4
"encoding/json"
5
"fmt"
6
"log"
7
"net/http"
8
"os"
9
"time"
10
)
11
12
func main() {
13
// Construct endpoint URL for listing devices in project.
14
deviceListUrl := "https://api.d21s.com/v2/projects"
15
16
// Create a custom http Client with timeout.
17
client := &http.Client{Timeout: time.Second * 3}
18
19
// Create the request object with method, URL, but no optional body.
20
req, err := http.NewRequest("GET", deviceListUrl, nil)
21
if err != nil {
22
log.Fatal(err)
23
}
24
25
// Set the request's Authorization header to use HTTP Basic Authentication.
26
req.SetBasicAuth(
27
os.Getenv("DT_SERVICE_ACCOUNT_KEY_ID"),
28
os.Getenv("DT_SERVICE_ACCOUNT_SECRET"),
29
)
30
31
// Send an HTTP request and return an HTTP response.
32
response, err := client.Do(req)
33
if err != nil {
34
log.Fatal(err)
35
}
36
defer response.Body.Close()
37
38
// Convert response body to map.
39
var data map[string]interface{}
40
json.NewDecoder(response.Body).Decode(&data)
41
42
// Print response body.
43
fmt.Println(data)
44
}
Copied!
1
curl -X GET "https://api.disruptive-technologies.com/v2/projects" \
2
-H "accept: application/json" \
3
-u $DT_SERVICE_ACCOUNT_KEY_ID:$DT_SERVICE_ACCOUNT_SECRET
Copied!
Last modified 2mo ago