Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Firestore REST API createDocument auto genarates ID

This is the REST API call I make to create a new document in my firestore database

POST https://firestore.googleapis.com/v1beta1/projects/[xxx]/databases/(default)/documents/items

Here is the payload that I add to the body of the post method:


    {
        "name": "projects/[xxx]/databases/(default)/documents/items/6001156905205",
        "fields": {
            "Name": {
                "stringValue": "Freshpak Rooibos Tea 80Pk"
            },
            "Description": {
                "stringValue": "Enjoy a cup of Freshpak Rooibos your no 1 Rooibos tea.
            }
        },
        "createTime": "2018-02-21T14:20:55.753234Z",
        "updateTime": "2018-02-21T14:20:55.753234Z"
    }

I get the below error message:


    {
        "error": {
            "code": 400,
            "message": "document.name must not be set",
            "status": "INVALID_ARGUMENT"
        }
    }

When I remove the "name" property from the payload it creates the record in firebase but the document Id is then auto generated. I would like the Id to be 6001156905205.

Am I doing something wrong?

Thanks Gert

like image 947
Gert Botha Avatar asked Feb 23 '18 04:02

Gert Botha


1 Answers

You need to specify the document ID that you want to use, as a query parameter, as detailed here. The createTime and updateTime are output only, so they will automatically be set by Cloud Firestore.

HTTP Request

POST https://firestore.googleapis.com/v1beta1/projects/[xxx]/databases/(default)/documents/items?documentId=6001156905205

Request body

{
  "fields": {
    "Name": {
      "stringValue": "Freshpak Rooibos Tea 80Pk"
    },
    "Description": {
      "stringValue": "Enjoy a cup of Freshpak Rooibos your no 1 Rooibos tea.
    }
  }
}
like image 148
Jason Berryman Avatar answered Oct 04 '22 05:10

Jason Berryman