Create Document Id (Contraints) for Firestore (Python)

Jun 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 base64value = "[email protected]"# optional casefold() to convert to lowercasedoc_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 hashlibvalue = "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.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.