> For the complete documentation index, see [llms.txt](https://docs.developer.disruptive-technologies.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.developer.disruptive-technologies.com/authentication/basic-auth.md).

# Basic Auth

## 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](/service-accounts/creating-a-service-account.md) 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.

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

Create and enter a new directory that will contain the example project.

{% tabs %}
{% tab title="Python 3.11" %}
Install the necessary dependencies.

```bash
requests==2.31.0
```

Create a file `main.py` with the following content.

```python
import os
import requests

if __name__ == '__main__':
    projects = 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(projects.json())
```

{% endtab %}

{% tab title="Node.js 20" %}
Install the necessary dependencies.

```bash
npm install axios@1.4.0
```

Create a file `index.js` with the following content.

```javascript
const axios = require('axios').default;

async function main() {
    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,
        }
    })

    console.log(JSON.stringify(response.data, null, 2))
}
main()
```

{% endtab %}

{% tab title="Go 1.20" %}
Initialize a new Go project.

```bash
go mod init example
```

Create a file `main.go` with the following content.

```go
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 body map[string]interface{}
	if err = json.NewDecoder(response.Body).Decode(&body); err != nil {
		log.Fatal(err)
	}

	// Pretty print the response body.
	prettyBody, _ := json.MarshalIndent(body, "", "    ")
	fmt.Println(string(prettyBody))
}

```

{% endtab %}

{% tab title="cURL" %}

```bash
curl -X GET "https://api.disruptive-technologies.com/v2/projects" \
    -H "accept: application/json" \
    -u $DT_SERVICE_ACCOUNT_KEY_ID:$DT_SERVICE_ACCOUNT_SECRET
```

{% endtab %}
{% endtabs %}

Running the file should list all projects available to the Service Account.

While this example sends a GET request to the API that lists all available projects, the request URL can be replaced with any call in our [REST API Reference](https://developer.disruptive-technologies.com/api).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.developer.disruptive-technologies.com/authentication/basic-auth.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
