Search code examples
node.jsnestjsazure-cosmosdb

How to create containers programmatically in Cosmos


I have the following code

this.client = new CosmosClient({
  endpoint: 'endpoint url',
  key: '<secret>',
})

this.container = this.client.database('database name').container('container name')

Which works fine.

However, now I need to programmatically or on-demand, create new containers using the JavaScript SDK, is that possible?


Solution

  • Yes you can create containers using the JS SDK for Azure CosmosDB NOSQL API,

    Sample code,

    await database.containers.createIfNotExists({ id: containerId });
    

    Also we have a sample in the SDK repository.