6

Hello I am looking to write a script which uses firebase firestore and writes some json to a specific collection in firestore. I have done this with the realtime db but firestore is a tad different below is my Realtime db snippet that works.

curl -X POST \
-d '{"param1":"'"$1"'", "param2":"'"$2"'"}' \
https://xxxx.firebaseio.com/xxxx.json?

Thanks for the help

Edward DiGirolamo
  • 742
  • 1
  • 5
  • 18
  • 2
    Cloud Firestore is a completely different database than the Firebase Realtime Database. The documentation for the Firestore REST API can be found here: https://firebase.google.com/docs/firestore/use-rest-api – Frank van Puffelen Oct 16 '17 at 03:58

3 Answers3

22

After Reading the documentation I got to this

curl -X POST \
-H "Content-Type: application/json" \
-d'{
"fields": {
"Field1": {
"stringValue": "'"$var1"'"
},
"Field2": {
"stringValue": "'"$var2"'"
},
"Field3": {
"stringValue": "$var3"
}
}
}'\"https://firestore.googleapis.com/v1beta1/projects/**PROJECT_ID**/databases/(default)/documents/**COLLECTION_ID**?&key=(YOUR API KEY)"
Edward DiGirolamo
  • 742
  • 1
  • 5
  • 18
6

The accepted answer helped me, but it took me a long time to figure out how can I use data types other than stringValues, so I am adding this answer hoping someone finds this helpful in the future.

curl -X POST \
-H "Content-Type: application/json" \
-d' {
    "fields": {
        "Field1": {
            "arrayValue": {
                "values": [{
                    "mapValue": {
                        "fields": {
                            "key1": {
                                "stringValue": "val1"
                            },
                            "key2": {
                                "stringValue": "val2"
                            }
                        }

                    }
                }]
            }
        },
        "Field2": {
            "integerValue": <intValue>
        },
        "Field3": {
            "stringValue": "var3"
        }
    }
}'\"https://firestore.googleapis.com/v1beta1/projects/**PROJECT_ID**/databases/(default)/documents/**COLLECTION_ID**?&key=<YOUR WEB API KEY>"

Use this for reference.

ashutosh sharma
  • 192
  • 2
  • 8
1

example field value

var data = { "fields": { "productName":{"stringValue": dealname.toString()}, "companyname":{"stringValue": companyName.toString()}, "contact":{"stringValue": contact.toString()}, "email":{"stringValue": email.toString()}, "domain":{"stringValue": domain.toString()}, "createdate":{"stringValue": createdate.toString()}, "salesCode":{"stringValue": code.toString()}, "price":{"stringValue": amount.toString()}, "phone":{"stringValue": phone.toString()}, "orderId":{"stringValue": orderId.toString() } } };

more information firestore information