<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Josué Martínez Buenrrostro</title>
    <description>The latest articles on DEV Community by Josué Martínez Buenrrostro (@josuemb).</description>
    <link>https://dev.to/josuemb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F114734%2F24a81fd4-2971-4934-995e-8a94598a7ae6.jpg</url>
      <title>DEV Community: Josué Martínez Buenrrostro</title>
      <link>https://dev.to/josuemb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/josuemb"/>
    <language>en</language>
    <item>
      <title>How to use Cosmos DB REST API with Postman in 2021</title>
      <dc:creator>Josué Martínez Buenrrostro</dc:creator>
      <pubDate>Mon, 23 Aug 2021 21:26:28 +0000</pubDate>
      <link>https://dev.to/josuemb/how-to-use-cosmos-db-rest-api-with-postman-in-2021-4db</link>
      <guid>https://dev.to/josuemb/how-to-use-cosmos-db-rest-api-with-postman-in-2021-4db</guid>
      <description>&lt;h2&gt;
  
  
  Introduction &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Using &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/" rel="noopener noreferrer"&gt;Cosmos DB REST API&lt;/a&gt; from &lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; can be a challenge.&lt;br&gt;
It involves send some mandatory &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/common-cosmosdb-rest-request-headers" rel="noopener noreferrer"&gt;request headers&lt;/a&gt;, mainly for authorization porpoises.&lt;/p&gt;

&lt;p&gt;You can give it a look  at:&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources&lt;/a&gt;&lt;br&gt;
As well as a C# Sample at: &lt;a href="https://github.com/Azure/azure-cosmos-dotnet-v2/tree/master/samples/rest-from-.net" rel="noopener noreferrer"&gt;https://github.com/Azure/azure-cosmos-dotnet-v2/tree/master/samples/rest-from-.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post help you to make it easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do you need? &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;An &lt;a href="https://docs.microsoft.com/en-us/azure/cosmos-db/introduction" rel="noopener noreferrer"&gt;Azure Cosmos DB Account&lt;/a&gt;:&lt;/strong&gt;
If you don't already have one, you can get it from free: &lt;a href="https://docs.microsoft.com/en-us/azure/cosmos-db/free-tier" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/azure/cosmos-db/free-tier&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Last version of &lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; installed:&lt;/strong&gt;
To install it, look at: &lt;a href="https://learning.postman.com/docs/getting-started/installation-and-updates/" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/getting-started/installation-and-updates/&lt;/a&gt;. You can use web app version instead: &lt;a href="https://learning.postman.com/docs/getting-started/installation-and-updates/" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/getting-started/installation-and-updates/&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to do it? &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To make process simple in Postman, we will be creating one collection with necessary &lt;a href="https://learning.postman.com/docs/sending-requests/variables/#defining-collection-variables" rel="noopener noreferrer"&gt;collection variables&lt;/a&gt; and a &lt;a href="https://learning.postman.com/docs/writing-scripts/pre-request-scripts/" rel="noopener noreferrer"&gt;pre-request script&lt;/a&gt; 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disclaimer &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;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 &lt;a href="https://learning.postman.com/docs/" rel="noopener noreferrer"&gt;Postman docs page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The process consist on the next steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get Cosmos DB URI and KEY.&lt;/li&gt;
&lt;li&gt;Create a new &lt;a href="https://learning.postman.com/docs/sending-requests/intro-to-collections/#creating-collections" rel="noopener noreferrer"&gt;Postman Collection&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create &lt;a href="https://learning.postman.com/docs/sending-requests/variables/#defining-collection-variables" rel="noopener noreferrer"&gt;Postman Collection variables&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create &lt;a href="https://learning.postman.com/docs/writing-scripts/pre-request-scripts/" rel="noopener noreferrer"&gt;pre-request script&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://learning.postman.com/docs/sending-requests/intro-to-collections/#adding-requests" rel="noopener noreferrer"&gt;Add a new request to the collection&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next we are going to see each step in a detailed way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Cosmos DB URI and KEY &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;You can go to the &lt;a href="https://portal.azure.com/" rel="noopener noreferrer"&gt;Azure Portal&lt;/a&gt;, 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.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywquq72iocix1actsnie.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywquq72iocix1actsnie.jpg" alt="Getting URI and KEY from Azure Portal"&gt;&lt;/a&gt;&lt;br&gt;
If you feel more comfortable using a shell, then you can get them using either &lt;a href="https://docs.microsoft.com/en-us/azure/cosmos-db/scripts/powershell/common/keys-connection-strings" rel="noopener noreferrer"&gt;Powershell&lt;/a&gt; or &lt;a href="https://docs.microsoft.com/en-us/cli/azure/cosmosdb/keys?view=azure-cli-latest" rel="noopener noreferrer"&gt;az&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a new Postman Collection &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;In this post, we are assuming that collection name will be "Cosmos DB REST".&lt;br&gt;
See hot to do it in: &lt;a href="https://learning.postman.com/docs/sending-requests/intro-to-collections/#creating-collections" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/sending-requests/intro-to-collections/#creating-collections&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Postman Collection variables &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;See how to do it in: &lt;a href="https://learning.postman.com/docs/sending-requests/variables/#defining-collection-variables" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/sending-requests/variables/#defining-collection-variables&lt;/a&gt;).&lt;br&gt;
An then create next variables:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;VARIABLE&lt;/th&gt;
&lt;th&gt;CURRENT VALUE&lt;/th&gt;
&lt;th&gt;NOTES&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;authorization_type&lt;/td&gt;
&lt;td&gt;master&lt;/td&gt;
&lt;td&gt;We are using Master Key Authorization but we can use a &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources?redirectedfrom=MSDN#authorization-header" rel="noopener noreferrer"&gt;different option&lt;/a&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;authorization_version&lt;/td&gt;
&lt;td&gt;1.0&lt;/td&gt;
&lt;td&gt;See &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources?redirectedfrom=MSDN#authorization-header" rel="noopener noreferrer"&gt;valid values&lt;/a&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;authorization_signature&lt;/td&gt;
&lt;td&gt;PRIMARY KEY value obtained in step Get Cosmos DB URI and KEY.&lt;/td&gt;
&lt;td&gt;See &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources?redirectedfrom=MSDN#authorization-header" rel="noopener noreferrer"&gt;valid values&lt;/a&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;x-ms-version&lt;/td&gt;
&lt;td&gt;2018-12-31&lt;/td&gt;
&lt;td&gt;See &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/#supported-rest-api-versions" rel="noopener noreferrer"&gt;supported REST API Versions&lt;/a&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvhbzu197kio9wj9qj6f5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvhbzu197kio9wj9qj6f5.jpg" alt="Collection Variables Example"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Important:&lt;/strong&gt; We prefer to set just "CURRENT VALUE" for each variable. Check why at &lt;a href="https://learning.postman.com/docs/sending-requests/managing-environments/#adding-environment-variables" rel="noopener noreferrer"&gt;Adding environment variables&lt;/a&gt; Postman documentation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create pre-request scripts &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;See what is it at: &lt;a href="https://learning.postman.com/docs/writing-scripts/pre-request-scripts/" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/writing-scripts/pre-request-scripts/&lt;/a&gt;&lt;br&gt;
The next pre-request script will test for mandatory variables, calculate and set valid headers for all the request in the collection.&lt;br&gt;
To see how to create a pre-request script in Postman see: &lt;a href="https://learning.postman.com/docs/writing-scripts/pre-request-scripts/#scripting-before-your-request-runs" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/writing-scripts/pre-request-scripts/#scripting-before-your-request-runs&lt;/a&gt;.&lt;br&gt;
To see GitHub Gist of the pre-request script go to: &lt;a href="https://gist.github.com/josuemb/f03232ac043dfa0ff645a367c69383e8" rel="noopener noreferrer"&gt;https://gist.github.com/josuemb/f03232ac043dfa0ff645a367c69383e8&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;GitHub Gist:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6sxxb3bz0gku4ay3jq7a.jpg" alt="Pre-request Script Example"&gt;

&lt;h3&gt;
  
  
  Add a new request to the collection &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;See how to do it in: &lt;a href="https://learning.postman.com/docs/sending-requests/intro-to-collections/#adding-requests" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/sending-requests/intro-to-collections/#adding-requests&lt;/a&gt;&lt;br&gt;
Since we are already creating all needed request headers in the &lt;a href="https://learning.postman.com/docs/writing-scripts/pre-request-scripts/" rel="noopener noreferrer"&gt;pre-request script&lt;/a&gt; (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).&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cosmos DB URI:&lt;/strong&gt; We already have seen how to get it in step 1. In this case it is: &lt;a href="https://jmbcosmosnotebookdemo.documents.azure.com:443/" rel="noopener noreferrer"&gt;https://jmbcosmosnotebookdemo.documents.azure.com:443/&lt;/a&gt; but we can abbreviate it as: &lt;a href="https://jmbcosmosnotebookdemo.documents.azure.com/" rel="noopener noreferrer"&gt;https://jmbcosmosnotebookdemo.documents.azure.com/&lt;/a&gt; since 443 is the standard port for https protocol.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/list-databases#request" rel="noopener noreferrer"&gt;List Databases URI&lt;/a&gt;:&lt;/strong&gt; In this case: &lt;a href="https://jmbcosmosnotebookdemo.documents.azure.com/dbs" rel="noopener noreferrer"&gt;https://jmbcosmosnotebookdemo.documents.azure.com/dbs&lt;/a&gt;. To see other operations, see: &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/common-tasks-using-the-cosmosdb-rest-api" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/rest/api/cosmos-db/common-tasks-using-the-cosmosdb-rest-api&lt;/a&gt; and to see valid URIs see: &lt;a href="https://docs.microsoft.com/en-us/rest/api/cosmos-db/cosmosdb-resource-uri-syntax-for-rest" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/rest/api/cosmos-db/cosmosdb-resource-uri-syntax-for-rest&lt;/a&gt;
That's it. Then we can execute the request and see response.
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcb6ifjfobo41v60cmt31.jpg" alt="List Databases Example"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;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.&lt;br&gt;
Get a Postman Collection example from: &lt;a href="https://www.getpostman.com/collections/57e0af5bf2d7021d4d15" rel="noopener noreferrer"&gt;https://www.getpostman.com/collections/57e0af5bf2d7021d4d15&lt;/a&gt;&lt;br&gt;
And then you can import it as is documented on: &lt;a href="https://learning.postman.com/docs/running-collections/working-with-data-files/" rel="noopener noreferrer"&gt;https://learning.postman.com/docs/running-collections/working-with-data-files/&lt;/a&gt;&lt;br&gt;
Feel free to reach me in twitter: &lt;a href="https://twitter.com/josuemb" rel="noopener noreferrer"&gt;@josuemb&lt;/a&gt;&lt;/p&gt;

</description>
      <category>azure</category>
      <category>cosmosdb</category>
      <category>postman</category>
      <category>rest</category>
    </item>
  </channel>
</rss>
