Postman Environment Variables – VMware Cloud Director

In a number of situations i need to turn to the API to maintain an object in VMware Cloud Director. Since version 9.x VCD has used a bearer auth token to allow access this is accessed using the /cloudapi/1.0.0/sessions/provider API. This will return a response header named X-VMWARE-VCLOUD-ACCESS-TOKEN that contains the auth token for that session. This token is then required for any further API calls that you wish to action. This post is a quicj guide on using Postman environment variables to capture this setting so you do not have to always copy and paste the token into your calls.

First step is to create a new environment. Click on the top right icon and then Add.

Name the Environment VCD and add a variable called accesstoken, you can leave the type and value fields default/blank. add vcdhostname and the hostname of your VCD API endpoint.

Set the dropdown to your new environment and create a new post session.

Setup a post call as you would normally to get the bearer token but in the url section note the use of the {{vcdhostname}}. Set the auth to basic auth and add the username and credentials (this can also be a environment variable).

Added the Accept header and the API version you want to use e.g. application/*;version=37.0 once again i have added this as a variable.

In the test tab add the below code. This will send the bearer token response to the variable.

var bearer = postman.getResponseHeader("X-VMWARE-VCLOUD-ACCESS-TOKEN")

Send the call and review the environment variable you should now see that the value is set.

You can now use this within other calls and do not have to keep updating the auth settings for each one e.g.

Running a Get cluster call using auth settings from the environment variable with {{accesstoken}}. I have also added a cluster UUID variable into the url.

Example Variables: