Create Cosmos Document


This tutorial talks about how to create documents in cosmos using the cosmos API. We can use Document APIs to manage or create document in Cosmos DB collection. You can refer to the link for cosmos APIs –  cosomos API

Authorization behavior for all the calls will be similar but we have to change verb as per our need, so we have to update Dataweave and HTTP request call to Azure.

Mule flow will look like this

Create a Cosmos Document


%dw 2.0
import java!cosmosdb::GenerateCosmosDBAuthToken
output application/java
var verb = "POST"
var resourceType = "docs" 
var resourceId = "dbs/hr/colls/employee"
var key = "0SZd1Zs3hySH915hKBTQCbewWnzIEI4S2YK8yHDaRwMrnzrZSwaa0hHy8IW21x4qbsQoswWIh3FW8TOd0fpSYw=="
var keyType = "master"
var tokenVersion = "1.0"		 
var serverDateTime = GenerateCosmosDBAuthToken::getServerTime() as String
	serverDateTime: serverDateTime,
	authKey: GenerateCosmosDBAuthToken::generate(verb, resourceType, resourceId, key, keyType, tokenVersion, serverDateTime)


Please change the resource id and key as per your Azure details

HTTP request

<http:request method="POST" doc:name="Cosmos DB API for SQL insert" doc:id="d1b8eda4-8576-48b2-ab08-c27bfa422779" config-ref="HTTP_Request_configuration" path="dbs/hr/colls/employee/docs">
        <http:headers ><![CDATA[#[output application/java
            "Accept" : "application/json",
            "x-ms-version" : "2016-07-11",
            "Authorization" : vars.cosmosHeader.authKey,
            "x-ms-date" : vars.cosmosHeader.serverDateTime,
            "Content-Type" : "application/json",
            "x-ms-documentdb-partitionkey" : '["' ++ payload.department ++ '"]'


Please see the path and different HTTP header we are passing in HTTP request call, please change the same as per your need

Test it through SOAP UI

Create a Cosmos Document


Documents in Azure

Create a Cosmos Document


Sample application – sample application

Sample SOAP UI project – Soap UI project


Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.

Share this:
Notify of
Inline Feedbacks
View all comments