DEV Community

Cover image for How to use Cosmos DB REST API with Postman in 2021
Josué Martínez Buenrrostro
Josué Martínez Buenrrostro

Posted on

How to use Cosmos DB REST API with Postman in 2021


Using Cosmos DB REST API from Postman can be a challenge.
It involves send some mandatory request headers, mainly for authorization porpoises.

You can give it a look at:
As well as a C# Sample at:

This post help you to make it easier.

What do you need?

  1. An Azure Cosmos DB Account: If you don't already have one, you can get it from free:
  2. Last version of Postman installed: To install it, look at: You can use web app version instead:

How to do it?

To make process simple in Postman, we will be creating one collection with necessary collection variables and a pre-request script to fill those variables before sending the request to the server, then in each request, we will be filling those variables with the corresponding variable values.


This is not a post oriented to learn Postman, then just expect to get general guidance instead of detailed steps, then I am putting the Postman documentation link into each step. Feel free to click on the links to see how to do it. Remember that you always can look at Postman docs page.

The process consist on the next steps:

  1. Get Cosmos DB URI and KEY.
  2. Create a new Postman Collection.
  3. Create Postman Collection variables.
  4. Create pre-request script.
  5. Add a new request to the collection.

Next we are going to see each step in a detailed way.

Get Cosmos DB URI and KEY

You can go to the Azure Portal, then click on your Cosmos BD account, and then go to the "🔑Keys" option into the "Settings" section and get: "URI" and either "PRIMARY KEY" or "SECONDARY KEY" (if you will be doing just read only operation it is preferable to use data from "Read-only Keys" tab.
Getting URI and KEY from Azure Portal
If you feel more comfortable using a shell, then you can get them using either Powershell or az.

Create a new Postman Collection

In this post, we are assuming that collection name will be "Cosmos DB REST".
See hot to do it in:

Create Postman Collection variables

See how to do it in:
An then create next variables:

authorization_type master We are using Master Key Authorization but we can use a different option.
authorization_version 1.0 See valid values.
authorization_signature PRIMARY KEY value obtained in step Get Cosmos DB URI and KEY. See valid values.
x-ms-version 2018-12-31 See supported REST API Versions.

Collection Variables Example
Important: We prefer to set just "CURRENT VALUE" for each variable. Check why at Adding environment variables Postman documentation.

Create pre-request scripts

See what is it at:
The next pre-request script will test for mandatory variables, calculate and set valid headers for all the request in the collection.
To see how to create a pre-request script in Postman see:
To see GitHub Gist of the pre-request script go to:
GitHub Gist:

Pre-request Script Example

Add a new request to the collection

See how to do it in:
Since we are already creating all needed request headers in the pre-request script (see step 4), then we just need to create a new request by indicating: verb, uri, body (optional) as well as additional request headers (just in case you need it).
Now we will create a new request to get the list the databases for a given Cosmos DB Account. To create it we will need:


Once you have done all previous steps, you can create any request and all needed request headers will be added automatically and you just need to add elements specific to each request.
Get a Postman Collection example from:
And then you can import it as is documented on:
Feel free to reach me in twitter: @josuemb

Top comments (3)

sandeeps123 profile image

Thanks for the guide, really useful!
A question please. If i follow the steps, and run the postman collection, i get the error - TypeError: Cannot read properties of null (reading '1')

But if I copy the Pre-request Script from the collection into the Request's pre-request script part, then it works!
Where could i be going wrong?

bigdata profile image
josuemb profile image
Josué Martínez Buenrrostro

It is similar but I simplified needed variables and changed the algorithm since the post you are referring did nor work for some cases.