firebasegoogle-cloud-firestore

Firestore REST API Query


I have a Firebase project, let's say: project-123. The project has users collection in Firestore database.

I can get all values from users with this API:

GET https://firestore.googleapis.com/v1/projects/project-123/databases/(default)/documents/users

But I want get values based on my filter query. I want get uid from exact email. Based on google, it direct me to these point:

  1. Use :runQuery method
  2. Use structuredQuery at the body

So I tried these: GET https://firestore.googleapis.com/v1/projects/project-123/databases/(default)/documents:runQuery

Payload:

{
    "structuredQuery": {
        "from": [
            {
                "collectionId": "users",
                "allDescendants": true
            }
        ],
        "where": {
            "fieldFilter": {
                "field": {
                    "fieldPath": "email"
                },
                "op": "EQUAL",
                "value": {
                    "stringValue": "emailsample@gmail.com"
                }
            }
        }
    }
}

But, whatever my payload, the response always:

That’s an error. Your client has issued a malformed or illegal request. That’s all we know.

Anyone got ideas to solve this? I need REST API way because I'm using directus as my backend.


Solution

  • As I see in your shared code, you're using the wrong HTTP method for the REST API call. According to the official documentation, the runQuery requires a POST request, not a GET:

    POST https://firestore.googleapis.com/v1/{parent=projects/*/databases/*/documents}:runQuery
    

    So, change the HTTP method so you can get the correct results.