2026, Jan 02 07:00

How to Generate Firebase Realtime Database Push Keys in Python Without Triggering a Write

Learn how to generate Firebase Realtime Database push IDs in Python without writing data. Use generatePushID locally to keep keys sortable and prevent writes.

Generating a Firebase Realtime Database key in Python without writing anything sounds straightforward, until you discover that calling push() actually writes a node. If you only need the key, not the side effect, you have to approach it differently.

The setup that triggers the issue

The intention is to obtain a Firebase-style, chronologically sortable key before writing the data. A typical attempt looks like this:

def make_fresh_id() -> str:
    return db.reference(app=my_fb_app).push().key
print(make_fresh_id())
# e.g. '-OOvws9uQDq9Ozacldjr'

This does return a key, but it also creates a child node in the database with an empty string as its value.

What actually happens

Each time push(value='') is used, a child is written. When called without explicitly passing a value, the write still happens, and the value defaults to an empty string. In the Python Admin SDK there isn’t a publicly exposed way to call push() purely for ID generation without performing the write.

The practical way to only get a key

If you need the key without modifying the database, the viable approach is to implement Firebase’s key generation algorithm locally. This is the same algorithm used broadly by clients and is considered stable. Below is a Python adaptation of generatePushID(), producing keys with the same format.

import time
import random
ID_LEXICON = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
def make_push_key():
    ms = int(time.time() * 1000)
    ts_symbols = []
    for _ in range(8):
        ts_symbols.append(ID_LEXICON[ms % 64])
        ms //= 64
    ts_symbols.reverse()
    token = ''.join(ts_symbols)
    for _ in range(12):
        token += random.choice(ID_LEXICON)
    return token
# Example
print(make_push_key())

This function mirrors the structure of Firebase push IDs by encoding the current timestamp into a base-64-like alphabet and appending additional characters.

Why this matters

Separating key generation from writes helps when you need to stage data locally, correlate IDs across services, or perform ordered batching without side effects. Since the Python Admin SDK does not offer a write-free push, implementing the algorithm locally lets you keep your data model intact while avoiding unintended database mutations. The algorithm is used widely, is very stable, and is unlikely to change because many clients depend on it.

Takeaways

If you only need a Firebase-style key in Python, do not call push() on a database reference, as it will create a node with an empty string value. Instead, generate the key locally using the algorithm shown above, and write data explicitly when you are ready. This keeps your database clean, preserves chronological ordering of keys, and avoids accidental writes during key generation.