Create Document Id (Contraints) for Firestore (Python)

June 2, 2019

Firestore Document ID Contraints

  • Must be valid UTF-8 characters
  • Must be no longer than 1,500 bytes
  • Cannot contain a forward slash (/)
  • Cannot solely consist of a single period (.) or double periods (..)
  • Cannot match the regular expression .*

NOTE: Refer this.

Auto Create

val doc_ref = db.collection("COLLECTION_NAME").document()
val doc_id =

Using Base64

If the document ID is short, like an email, you can convert it to base64 url safe.

Python 3.x

import base64

value = ""

# optional casefold() to convert to lowercase
doc_id = base64.urlsafe_b64encode(value.casefold().encode('utf-8')).decode('utf-8')



Using MD5

If the String is long or unpredictable.

Python 3.x

import hashlib

value = "This is my id"

doc_id = hashlib.md5(value.encode("utf-8")).hexdigest()



NOTE: MD5 hex output will always be 32 characters regardless of the input value.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.