Use Firestore in Datastore mode (Datastore)

Firestore is a NoSQL document database built for automatic scaling,high performance, and ease of application development. It is the newest versionof Datastore and introduces several improvements overDatastore.BecauseFirestore in Datastore mode (Datastore) is optimized for server use cases and for App Engine, we recommend usingDatastore for databases that will be used primarily byApp Engine apps. Firestore in Native mode is most useful formobile and real-time notification use cases. For more information aboutFirestore modes, seeChoosing between Native Mode andDatastore mode.

This document describes how to use theCloud Client Libraries to store andretrieve data in a Datastore mode database.

Prerequisites and setup

  • Set up your environment and project to understand howapps are structured in App Engine. Write down and save your project ID, becauseyou will need it to run the sample application described in this document.

Clone the repository

Download (clone) the sample:

  git clone https://github.com/GoogleCloudPlatform/python-docs-samples  cd python-docs-samples/appengine/flexible/datastore

Edit project configuration and set dependencies

Include thegoogle-cloud-datastore library inrequirements.txt file. Thisis the client library for Datastore mode.

Flask==3.0.3google-cloud-datastore==2.20.2gunicorn==23.0.0

Application code

The sample application logs, retrieves, and displays visitor IPs. You can seethat a log entry is a two-field class that is given the typevisit,and is saved to Datastore mode using theputcommand. Then, the ten most recent visits are retrieved in descending orderusing thequery() command.

@app.route("/")defindex():ds=datastore.Client()user_ip=request.remote_addr# Keep only the first two octets of the IP address.ifis_ipv6(user_ip):user_ip=":".join(user_ip.split(":")[:2])else:user_ip=".".join(user_ip.split(".")[:2])entity=datastore.Entity(key=ds.key("visit"))entity.update({"user_ip":user_ip,"timestamp":datetime.datetime.now(tz=datetime.timezone.utc),})ds.put(entity)query=ds.query(kind="visit",order=("-timestamp",))results=[]forxinquery.fetch(limit=10):try:results.append("Time:{timestamp} Addr:{user_ip}".format(**x))exceptKeyError:print("Error with result format, skipping entry.")output="Last 10 visits:\n{}".format("\n".join(results))returnoutput,200,{"Content-Type":"text/plain; charset=utf-8"}

Usingindex.yaml files

The sample app performs queries. More elaborateDatastore mode queries require one or moreindexes, which you must specify in anindex.yaml filethat you upload along with your app. This file may be created manually, orgenerated automatically while testing your app locally.

Local testing

If you need to develop and test your application locally, you canuse theDatastore mode emulator.

For more information

For complete information on Datastore mode, includingoptimizations and concepts, see theFirestore in Datastore mode documentation.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.