A guide on how to use Basic Auth for authenticating to our REST APIs.
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.
We don't recommend using this authentication flow for production use, the main use for Basic Auth is for quick exploration and experimentation.
Prerequisites
A Service Account must be created in the organization before continuing.
Code Sample
The following examples sends a GET request to list available spaces in your organization. See the API Reference for all available API calls.
import osimport requests # pip install requests# Inputskey_id ='<service account key id>'sercret ='<service account secret>'if__name__=='__main__':# Send GET request to endpoint of choice with Basic Auth authentication. spaces = requests.get( url='https://app.neowit.io/api/space/v1/space', auth=(key_id, secret), )# Print response contents.print(spaces.json())
// modulesconstaxios=require('axios').default; // npm install axios// InputsconstkeyID='<service account key id>';constsecret='<service account secret>';asyncfunctionmain() {// Send GET request to endpoint of choice with Basic Auth authentication.constresponse=awaitaxios({ method:'GET', url:'https://app.neowit.io/api/space/v1/space', auth: { username: keyId, password: secret, } })// Print response contents.console.log(JSON.stringify(response.data,null,2))}main();
packagemainimport ("encoding/json""fmt""log""net/http""os""time")// Inputs;const ( spacesURL ="https://app.neowit.io/api/space/v1/space" keyID ="<service account key id>" secret ="<service account secret>")funcmain() {// 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", spacesURL, nil)if err !=nil { log.Fatal(err) }// Set the request's Authorization header to use HTTP Basic Authentication. req.SetBasicAuth(keyID, 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))}