How to use update a cosmos db document using rest call

Saurabh Sharma 23,866 Reputation points Microsoft Employee Moderator
2025-11-13T01:23:39.9933333+00:00

Hi,
I want to update a cosmos db document using available cosmodb rest api.

Can you provide me a sample code or documentation for the same.

Thanks

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2025-11-13T01:24:24.7233333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To update a document in Azure Cosmos DB using a REST API call, you would typically use the PUT method. Below is a sample request format for updating a document:

    Request Format

    Method: PUT

    Request URI:
    https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs/{doc-id}
    Replace {databaseaccount}, {db-id}, {coll-id}, and {doc-id} with your specific values.

    Headers

    You will need to include the following headers in your request:

    • x-ms-documentdb-partitionkey: Required if your collection is partitioned.
    • If-Match: Optional, used for optimistic concurrency control. Set this to the etag value of the document you are updating.

    Body

    The body of the request should contain the updated document in JSON format. It must include the id property, which is a unique identifier for the document. Here’s an example of the body:

    {
      "id": "your-document-id",
      "property1": "new value",
      "property2": "new value"
    }
    

    Example

    Here’s a complete example of how your request might look:

    PUT https://your-account.documents.azure.com/dbs/your-db/colls/your-collection/docs/your-document-id
    x-ms-documentdb-partitionkey: ["your-partition-key"]
    If-Match: "etag-value"
    Content-Type: application/json
    
    {
      "id": "your-document-id",
      "property1": "new value",
      "property2": "new value"
    }
    

    Response

    A successful update will return a 200 OK status code along with the updated document body in the response.

    For more detailed information, you can refer to the official documentation on replacing a document in Azure Cosmos DB.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.