Azure COSMOS DB REST API integration
A Node-RED flow, to integrate Azure COSMOS DB with Node-RED using REST APIs exposed by Azure. No additional libraries used.
Checkout this video for more details:
[{"id":"f6f2187d.f17ca8","type":"tab","label":"Cosmos DB Test","disabled":false,"info":"","env":[{"name":"AzureMasterKey","value":"OoLzEQaRtYk9LCAvLEzhvmxLxSrCa7ZG6Y5OJL3YDEvU0W3kXNqqJykSiLShfgahbgqoCo2OOyqyACDbtPL1Xw==","type":"str"},{"name":"URI","value":"https://wgl.documents.azure.com:443","type":"str"},{"name":"Database","value":"","type":"str"},{"name":"Container","value":"","type":"str"}]},{"id":"fd143043be17f969","type":"junction","z":"f6f2187d.f17ca8","x":440,"y":360,"wires":[["9f4e3cd267349739"]]},{"id":"08f4ebc722d89f46","type":"http request","z":"f6f2187d.f17ca8","name":"","method":"use","ret":"obj","paytoqs":"body","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":850,"y":220,"wires":[["07d3342444cfdd7a"]]},{"id":"4754ab68196d6af9","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":40,"wires":[["b8fe5b02df9ad76a"]]},{"id":"07d3342444cfdd7a","type":"debug","z":"f6f2187d.f17ca8","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1020,"y":220,"wires":[]},{"id":"9f4e3cd267349739","type":"function","z":"f6f2187d.f17ca8","name":"Generate Headers with Auth","func":"// Read master key from the env.\nvar keyFromEnv = env.get(\"AzureMasterKey\");\nvar URI = env.get(\"URI\")\nif (!keyFromEnv) {\n throw \"Azure master key is not set. Please configure `AzureMasterKey` in flow config.\"\n}\nif (!URI) {\n throw \"Azure URI is not set. Please configure `URI` in flow config.\"\n}\n// Read input config from msg.payload\nvar apiDetails = msg.config;\nvar dateString = new Date().toUTCString();\n\nvar key = Buffer.from(keyFromEnv, \"base64\");\n\nmsg.url = apiDetails.endPoint;\nmsg.method = apiDetails.verb;\nmsg.url = !!apiDetails.URI ? URI + \"/\" + apiDetails.URI : URI + \"/\" + apiDetails.resourceId;\n// Generate Signature Hash\nvar text = (apiDetails.verb || \"\").toLowerCase() + \"\\n\" +\n (apiDetails.resourceType || \"\").toLowerCase() + \"\\n\" +\n (apiDetails.resourceId || \"\") + \"\\n\" +\n dateString.toLowerCase() + \"\\n\" +\n \"\" + \"\\n\";\n\nvar body = Buffer.from(text, \"utf8\");\nvar signature = crypto.createHmac(\"sha256\", key).update(body).digest(\"base64\");\n\n// Generate auth header\nvar auth = encodeURIComponent(\"type=master&ver=1&sig=\" + signature)\n\nmsg.headers = {\n ...msg.headers,\n ...{\n \"x-ms-version\": \"2018-12-31\",\n authorization: auth,\n \"x-ms-date\": dateString,\n \"x-ms-documentdb-isquery\": true\n }\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"crypto","module":"crypto"}],"x":600,"y":220,"wires":[["08f4ebc722d89f46","65cfe728c89aa38f"]],"info":"The expected input to the node is \r\n\r\n```js\r\n msg.config = {\r\n verb:\"\",\r\n resourceType:\"\",\r\n resourceId:\"\"\r\n }\r\n```\r\n\r\n\r\nOutputs msg.headers"},{"id":"b8fe5b02df9ad76a","type":"function","z":"f6f2187d.f17ca8","name":"List Databases","func":"msg.config = {\n verb: \"get\",\n resourceType: \"dbs\",\n resourceId: \"\",\n URI:\"dbs\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":40,"wires":[["9f4e3cd267349739"]]},{"id":"06e0b6288c772f22","type":"function","z":"f6f2187d.f17ca8","name":"Get Database","func":"var db = env.get('Database');\nmsg.config = {\n verb: \"get\",\n resourceType: \"dbs\",\n resourceId: \"dbs/\"+db,\n URI:\"dbs/\"+db\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":120,"wires":[["9f4e3cd267349739"]]},{"id":"1ccaaadce06619d1","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":120,"wires":[["06e0b6288c772f22"]]},{"id":"a35d8b812be2b9aa","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":180,"wires":[["0cf12bb2e60d62f7"]]},{"id":"0cf12bb2e60d62f7","type":"function","z":"f6f2187d.f17ca8","name":"List Collections","func":"var db = env.get('Database');\nmsg.config = {\n verb: \"get\",\n resourceType: \"colls\",\n resourceId: \"dbs/\",\n URI:\"dbs/\"+db+\"/colls\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":180,"wires":[["9f4e3cd267349739"]]},{"id":"37cfd13478aca55b","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":240,"wires":[["b6ca14835adc9e8b"]]},{"id":"b6ca14835adc9e8b","type":"function","z":"f6f2187d.f17ca8","name":"Get aCollection","func":"var db = env.get('Database');\nvar collection = env.get('Container');\n\nmsg.config = {\n verb: \"get\",\n resourceType: \"colls\",\n resourceId: \"dbs/\"+db+\"/colls/\"+collection,\n URI:\"dbs/\"+db+\"/colls/\"+collection\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":240,"wires":[["9f4e3cd267349739"]]},{"id":"712b6be5150b8531","type":"function","z":"f6f2187d.f17ca8","name":"Li Documents","func":"var db = env.get('Database');\nvar collection = env.get('Container');\n\nmsg.config = {\n verb: \"get\",\n resourceType: \"docs\",\n resourceId: \"dbs/\"+db+\"/colls/\"+collection,\n URI:\"dbs/\"+db+\"/colls/\"+collection+\"/docs\"\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":300,"wires":[["9f4e3cd267349739"]]},{"id":"b7198c3934a83db0","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":300,"wires":[["712b6be5150b8531"]]},{"id":"a705d2ed35f40b93","type":"function","z":"f6f2187d.f17ca8","name":"Get a Document","func":"var db = env.get('Database');\nvar collection = env.get('Container');\n\nmsg.config = {\n verb: \"get\",\n resourceType: \"docs\",\n resourceId: \"dbs/\"+db+\"/colls/\"+collection+\"/docs/Wakefield.7\",\n URI:\"dbs/\"+db+\"/colls/\"+collection+\"/docs/Wakefield.7\"\n}\n\nmsg.headers = {\n \"x-ms-documentdb-partitionkey\": '[\"Italy\"]'\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":360,"wires":[["fd143043be17f969"]]},{"id":"67d1ff136058822d","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":360,"wires":[["a705d2ed35f40b93"]]},{"id":"811649e1f17a2a85","type":"function","z":"f6f2187d.f17ca8","name":"Create a Document","func":"var db = env.get('Database');\nvar collection = env.get('Container');\n\nmsg.config = {\n verb: \"post\",\n resourceType: \"docs\",\n resourceId: \"dbs/\" + db +\"/colls/\"+collection,\n URI: \"dbs/\" + db +db+\"/colls/\"+collection+\"/docs\"\n}\nmsg.headers = {\n \"x-ms-documentdb-partitionkey\": '[\"India\"]'\n}\nmsg.payload = {\n \"id\": \"AndersenFamily\",\n \"LastName\": \"Andersen\",\n \"partitionKey\": \"India\",\n \"Parents\": [\n {\n \"FamilyName\": null,\n \"FirstName\": \"Thomas\"\n },\n {\n \"FamilyName\": null,\n \"FirstName\": \"Mary Kay\"\n }\n ],\n \"Children\": [\n {\n \"FamilyName\": null,\n \"FirstName\": \"Henriette Thaulow\",\n \"Gender\": \"female\",\n \"Grade\": 5,\n \"Pets\": [\n {\n \"GivenName\": \"Fluffy\"\n }\n ]\n }\n ],\n \"Address\": {\n \"State\": \"WA\",\n \"County\": \"King\",\n \"City\": \"Seattle\"\n },\n \"IsRegistered\": true\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":420,"wires":[["fd143043be17f969"]]},{"id":"d01af63ace269d86","type":"inject","z":"f6f2187d.f17ca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":420,"wires":[["811649e1f17a2a85"]]},{"id":"65cfe728c89aa38f","type":"debug","z":"f6f2187d.f17ca8","name":"debug 4","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":300,"wires":[]},{"id":"134febada3138fcb","type":"comment","z":"f6f2187d.f17ca8","name":"Docs: https://learn.microsoft.com/en-us/rest/api/cosmos-db/","info":"Docs: https://learn.microsoft.com/en-us/rest/api/cosmos-db/","x":700,"y":60,"wires":[]}]