Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore REST example

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

like image 577
Edward DiGirolamo Avatar asked Nov 28 '22 20:11

Edward DiGirolamo


2 Answers

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)"
like image 111
Edward DiGirolamo Avatar answered Mar 16 '23 00:03

Edward DiGirolamo


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.

like image 42
ashutosh sharma Avatar answered Mar 15 '23 23:03

ashutosh sharma