Getting started with Xi IoT API

We believe, API is the primary mechanism for provisioning planet-scale IoT infrastructure and deploying AI/Edge Computing Apps. We are very excited to introduce Xi IoT API documentation so that you can automate your IoT infrastructure and application management tasks. The Xi IoT APIs follow RESTFul design. Here are quick tips to get started:

1. Create API User
You will need to create an API user with appropriate privileges. For example, if you need to automate Infrastructure workflows like “Add or Remove Edge”, “Create or edit Projects”, “Add or remove Datasources” etc, create a user with “Infrastructure Admin” privileges. If you need to automate Project User workflows like “Add or edit an Application and Data Pipeline”, “Add or remove Functions and Runtimes” etc, create a user with “Project User” privileges and assign a project to this user. Follow the Xi IoT Product documentation to create the required user, and setup permissions.

2. Setup API Authentication
Xi IoT REST API use “Bearer Token” in the HTTP Request header. You can obtain a Bearer token by calling “login” API. See login API documentation. Here is sample Python 3 code for obtaining a Bearer token. Let’s say the user is steve@numart.com 
import http.client 
conn = http.client.HTTPSConnection("iot.nutanix.com")
payload = "{\"email\":\"steve@numart.com",\"password\":\"password\"}"
headers = { 'content-type': "application/json" } conn.request("POST","/v1.0/login", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Sample Response:

{
   "token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InN0ZXZlQG51bWFydC5jb20iLCJleHAiOjE1NTM3MDg3MTgsImlkIjoiNWExMGU3MmQtMjBkZi00ZDM1LWE2NjctZGMyMTcwYjgxNTI3IiwibmFtZSI6ImRlbW8iLCJuYmYiOjE0NDQ0Nzg0MDAsInJvbGVzIjpbXSwic2NvcGVzIjpbXSwic3BlY2lhbFJvbGUiOiJhZG1pbiIsInRlbmFudElkIjoidGVuYW50LWlkLW51bWFydC1zdG9yZXMifQ.KfTvwMHcDv1n0A09d3k07TiOhVCAuHnoKrB8kMgS3S0",
   "_id":"5a10e72d-20df-4d35-a667-dc2170b81527",
   "name":"Steve",
   "email":"steve@numart.com"
}
You can use this “token” as Authentication for all Xi IoT APIs.

3. Get list of Projects
Now that you have Bearer token, let’s get list of Projects in your account. (Every account has “Default” project unless you deleted it.). Make sure to replace Bearer token in the example below with the token you obtained in step 2 above.
import http.client 
conn = http.client.HTTPSConnection("iot.nutanix.com")
headers = { 'authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImRlbW9AbnVtYXJ0LmNvbSIsImV4cCI6MTU1MzcwODcxOCwiaWQiOiI1YTEwZTcyZC0yMGRmLTRkMzUtYTY2Ny1kYzIxNzBiODE1MjciLCJuYW1lIjoiZGVtbyIsIm5iZiI6MTQ0NDQ3ODQwMCwicm9sZXMiOltdLCJzY29wZXMiOltdLCJzcGVjaWFsUm9sZSI6ImFkbWluIiwidGVuYW50SWQiOiJ0ZW5hbnQtaWQtbnVtYXJ0LXN0b3JlcyJ9.tfxi2d2kHdPG8A9vQ49DXJj45j8rAeUqq-DdKg2jZA8" }
conn.request("GET", "/v1.0/projects", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
 
Wasn’t that simple! What will you automate with Xi IoT API?
With this example, you can get started with Xi IoT API set. Explore more APIs at official API documentation site. We would love to hear about your experience using the API, suggestions on improving the documentation and so on. Drop us a note at iot-api at nutanix DOT com. Have fun automating your IoT Infrastructure and Applications!