Description

This article explains how to use Postman in testing and developing your Juniper Apstra automations.

Juniper Apstra APIs are built on Swagger. The Swagger API documentation is available from the Juniper Apstra UI by navigating to Platform > Developers > REST API documentation. Typically the URL will be https://{ YOUR_APSTRA_SERVER }/swagger-ui/?url=/api/docs . Here you can go through the available APIs and try them out. Remember, everything that is done through the UI can be automated by using the APIs.

Examples in this article were tested against Juniper Apstra v4.1.2 and Postman v10.12.6, but will likely work in later versions. It is assumed that the user has access to a working Juniper Apstra server.

Juniper Apstra APIs use HTTPS POST, GET, PATCH, and PUT calls. The returned data is in JSON format and can be processed by using Postman Test scripts for further use in your collection.

Solution

 

Note: A space has been included after the opening curly bracket and before the closing curly bracket in this article for the sake of format and content retention. If you are planning to copy the text within the curly brackets for use in a script or CLI or a browser, remember to remove the spaces.
 

Environment

It is recommended (but not necessary) to set up a Juniper Apstra environment in Postman. Create a new entry named "aos" and create the following key-value pair:

aos_server_apihttps://{ YOUR_APSTRA_SERVER }/api

Substitute { YOUR_APSTRA_SERVER } with the IP address or FQDN of your server.

Authentication

In order to make changes or query data, automation needs to be authenticated. Make a POST call to obtain an Auth Token, as can be seen on https://{ YOUR_APSTRA_SERVER }/swagger-ui/?url=/api/docs#/user/post_api_user_login.

In Postman, perform the following:

  1. Create a POST to {{ AOS_SERVER_API }}/user/login

  2. Under "Body," create the following snippet (replace credentials with the ones from your server's UI). You can also use Postman environment key-value pairs here:

{
"username": "admin",
"password": "admin"
}
  1. Under "Tests," create the following test script:

tests["Response is OK"] = (responseCode.code == 201);
if (responseCode.code == 201) {
try {
var login_results = JSON.parse(responseBody);
var token = login_results.token;
console.log("Token: ", token);
}
catch(e) {
console.log(e);
}
postman.setEnvironmentVariable("AOS_TOKEN", token);
}

When executed, the response is processed by the test script. If it receives a correct response, it will parse the response body, and from that create an environment variable that can be used for authentication of subsequent API calls. The response should look something like this:

{
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiY3JlYXRlZF9hdCI6IjIwMTktMDktMTJUMTQ6MDQ6MzguNDE4OTAyIiwidXNlcl9zZXNzaW9uIjoiMGM5NWU1M2MtNzMwMi00ODYwLWE1NTUtY2QyMjgyYWU3YjUxIiwiZXhwIjoxNTY4MzgzNDc4fQ.esftT_ulczZf6N7SOLwlgf4TwmGh5EXbM34GJ3_3Qjw",
"id":"86066651-f723-4617-9b6f-1c7830263238"
}

In your Postman environment, there should be a variable named AOS_TOKEN with the same value. 

Example

Create a new Blueprint by using the APIs. Blueprints are created from Templates, so in order for the creation to be successful, you need to pass a valid Template name through the API call.

In Postman, create the following:

  1. POST to {{ AOS_SERVER_API }}/blueprints

  2. Under Headers, create the following two entries:

AUTHTOKEN
{{ AOS_TOKEN }}
Content-Type
 application/json

This ensures that the previously collected Auth token and content type are passed through the headers.

  1. In the Body, create the following snippet:

{
"init_type": "template_reference",
"design": "two_stage_l3clos",
"template_id": "L2_Virtual_EVPN",
"label": "L2 Virtual EVPN"
}

Leave init_type and design to the above values. For template_id , you can choose any existing template. Find the correct ID by browsing to the template and getting the ID from the URL, for example:

https://{ YOUR_APSTRA_SERVER }/#/design/templates/L2_Virtual_EVPN

The label is freeform.

  1. Under Tests, create the following script:

tests["Response is OK"] = (responseCode.code == 201);
if (responseCode.code == 201) {
try {
var bp_results = JSON.parse(responseBody);
var bp_id = bp_results.id;
console.log("BP ID: ", bp_id);
}
catch(e) {
console.log(e);
}
postman.setEnvironmentVariable("bp_id", bp_id);
}

After checking the response, this will parse the returned data and create a new environment variable in Postman named " bp_id" , with the value set to the ID.

  1. Press Send . The new Blueprint should be visible in the UI immediately. The response body should look something like this:

{
"id":"0f7a2cfc-d905-486c-9e1a-53964a7ce2d4"
}

From here on, you can add additional API calls to your collection, using AUTH_TOKEN for authentication and bp_id to refer to your new Blueprint. Other calls may not require the Blueprint ID, or may require other variables, for example Device IDs or External Routers. You can create your own test scripts to collect and set variables similar to the above. Remember, all operations made from the UI can also be made through an API call.

Postman can also be used to run entire collections to make several API calls in a batch.

Modification History

2023-04-06: Reviewed, minor non-technical updates and corrections.