Nutanix Intentful API
Release 5.11.2
Home
Introduction
Getting Started
Authentication
Use Cases
network_security_rules
access_control_policies
batch
blackouts
whatif
categories
clusters
reports
directory_services
hosts
idempotence_identifiers
identity_providers
images
image_placement_policies
network_function_chains
alerts
ngt_policies
permissions
projects
protection_rules
recovery_plan_jobs
recovery_plans
remote_syslog_modules
remote_syslog_servers
roles
subnets
tasks
user_groups
users
vms
Powered by Stoplight

AUTHENTICATION

All requests to the Nutanix REST APIs must be authenticated. The following authentication types are supported.

  • Basic Authentication: The user provides username and password every time a request is sent as the authentication header.
  • Session Authentication: The user credentials are stored in a cookie. The following are the code snippets to authenticate a user.

Python

#!/usr/bin/env python3.7
import urllib3
import requests
from base64 import b64encode
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
request_url = 'https://10.0.0.1:9440/api/nutanix/v3/vms/list'
username = 'admin'
password = 'password'
encoded_credentials = b64encode(bytes(f'{username}:{password}',
encoding='ascii')).decode('ascii')
auth_header = f'Basic {encoded_credentials}'
payload = '{"kind":"vm"}'
headers = {'Accept': 'application/json', 'Content-Type': 'application/json',
'Authorization': f'{auth_header}', 'cache-control': 'no-cache'}
response = requests.request('post', request_url, data=payload, headers=headers,
verify=False)
print(response.status_code)

C#

using System;
using System.Net;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string ClusterUsername = "admin";
string ClusterPassword = "Ntnx2017!";
string RequestUrl = "https://192.168.1.131:9440/api/nutanix/v3/vms/list";
string Payload = "{\"kind\":\"vm\"}";
string AuthHeader = System.Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
ClusterUsername + ":" + ClusterPassword));
ServicePointManager.ServerCertificateValidationCallback = (
(sender, certificate, chain, sslPolicyErrors) => true);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
String authHeader = System.Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
ClusterUsername + ":" + ClusterPassword));
var request = (HttpWebRequest)WebRequest.Create(RequestUrl);
var requestBody = Encoding.ASCII.GetBytes(Payload);
HttpWebResponse HttpResponse = null;
request.Headers.Add("Authorization", "Basic " + AuthHeader);
request.Headers.Add("Accept-Encoding", "application/json");
request.ContentType = "application/json";
request.Accept = "application/json";
request.Method = "POST";
var newStream = request.GetRequestStream();
newStream.Write(requestBody, 0, requestBody.Length);
newStream.Close();
HttpResponse = (HttpWebResponse)request.GetResponse();
Console.WriteLine(HttpResponse.StatusCode);
Console.ReadKey();
}
}
}

PowerShell

# set the basic properties for the request
$RequestUri = "https://10.0.0.1:9440/api/nutanix/v3/vms/list"
$Payload = "{""kind"":""vm""}"
$Username = "admin"
$Password = "password"
# create the HTTP Basic Authorization header
$pair = $Username + ":" + $Password
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
# setup the request headers
$Headers = @{
'Accept' = 'application/json'
'Authorization' = $basicAuthValue
'Content-Type' = 'application/json'
}
# disable SSL certification verification
# you probably shouldn't do this in production ...
if (-not (
[System.Management.Automation.PSTypeName] `
'ServerCertificateValidationCallback').Type)
{$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# submit the request
Invoke-WebRequest `
-Uri $RequestUri `
-Headers $Headers `
-Method "POST" `
-Body $Payload `
-TimeoutSec 5 `
-UseBasicParsing `
-DisableKeepAlive `
| ConvertFrom-Json | Select *

PHP

<?php
$username = 'admin';
$password = 'password';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_URL => 'https://10.0.0.1:9440/api/nutanix/v3/vms/list',
CURLOPT_USERPWD => $username . ':' . $password, CURLOPT_POST => 1, CURLOPT_SSL_VERIFYHOST => FALSE, CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept-Encoding: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'kind' => 'vm'
])
]);
$result = curl_exec($ch);
curl_close($ch);
if(!$result) {
die('Error: "' . curl_error($ch) . '". Code: ' . curl_errno($ch));
}
else {
echo($result);
}