Firestore REST API

Using Node.js

I was having a little bit of a struggle with using Firestore REST API for the first time yesterday and I thought I’d share my results for those who are looking to use Firestore REST API. I will keep on updating this post as I try out more methods.

Creating a Document

The three important parts are: (1) request body, (2) path and (3) authorization.

Request Body

For convenience sake, I initialized the data below, but obviously, you’d be getting it through req.body. It’s important to have your data in {fields: data} format, and declare the data type you’ll be using. Notice that for mapValue, I used {fields: data} again.

request body

Path

If you just want to create a collection & document, it’s straight forward. If you don’t specify the document Id (using the query ?documentId=<document_id>), Firestore will automatically create one for you.

https://firestore.googleapis.com/v1/projects/<project_id>/databases/(default)/documents/<collection_id>?documentId=<document_id>// Here's what I used
https://firestore.googleapis.com/v1/projects/<project_id>/databases/(default)/documents/users?documentId=sookim482

Then, the usual axios.post method.

axios.post()

The resulting res.data and Firestore should look like this:

res.data
Firestore

To create collection — document — collection — document, you just follow a similar format. Here, I want users (collection) → todos (document) → <user id> (collection) → <todo id> (document)

Authorization

The above is fine when you haven’t set any access restrictions in your Firestore rules. However, if you want to limit who can read and/or write, you need to attach an Authorization header, and set it to the user’s Firebase id token that is given when he/she signs up.

const _header = { 
headers: {
Authorization: `Bearer ${req.body.id_token}`,
},
}

In your Firestore rules, you can limit access. So, if you do not attach the header, or if the id_token is incorrect, you will get 403 error code (PERMISSION_DENIED).

Firestore Rules

Getting All Documents in a Collection

This is the most simple form of getting data and all you need is a proper path and the authorization header (if you’ve limited access).

https://firestore.googleapis.com/v1/projects/<project_id>/databases/(default)/documents/<collection_id>/<documentId>

Receiving the Data

The response data should like something like this.

res.data

Since all I need are the fields, and the key/value in fields (the fields come as key: { stringValue: value }), I simplified the process of sending it back to the front-end.

get all documents

Patching Only Specific Fields

This one uses a PATCH request. The endpoint requires updateMask query in the form of updateMask.fieldPaths=… If you put a field that isn’t in the original document, it will be added. The body is in the same format as in the “creating a document ” above.

https://firestore.googleapis.com/v1/projects/<project_id>/databases/(default)/documents/<collection_id>/<documentId>?updateMask.fieldPaths=title&updatedMask.fieldPaths=updatedTime

--

--

--

Flutter & Node.js Full-Stack Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

MEAN (3)

JavaScript learning corner weekly updates (3.24.22)

Styling a “react-mentions” Component With JSS

Profit from TypeScript in pure JavaScript projects

How to create a customized React hook package and publish it to npm?

HTML5, Browser Limitations and Conditional Javascript Statements: Best Practices

Creating Reusable & Configurable Angular Components

Professional engineer architect worker with protective helmet and blueprints paper at house building construction site background.

Javascript with Rails API project

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Soo Kim

Soo Kim

Flutter & Node.js Full-Stack Developer

More from Medium

How to Sign In with Google in Node.js

Set up NodeJs Express with Typescript

Automated Backups with Node.js and Cron

Integrate NodeJS application with Redis Cluster