Getting started with Xi IoT API
The Xi IoT team believes that an API is the primary mechanism for provisioning planet-scale IoT infrastructure and for deploying AI/Edge computing apps. As a result, 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. Consider these quick tips to get started.
- Create an API User
Create an API user with appropriate privileges.
If you need to automate Infrastructure workflows like Add or Remove Edge, Create or Edit Projects, Add or Remove Data Sources, and others, 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, and others, 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 set up those permissions.
2. Set Up API Authentication
The Xi IoT REST API use Bearer Token in the HTTP Request header. You can obtain a Bearer Token by calling the login API (as described in the login API documentation). This sample Python 3 code obtains a Bearer token for user 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 a List of Your Projects
Now that you have Bearer token, let’s get a list of Projects in your account with the GET /v1.0/projects API call. (Every account initially has a 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?
This example lets you get started with the Xi IoT API set. Explore more APIs at our 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!