Simple CRUD API Async Database


In this example you can see how to build a simple CRUD API using LaunchFlow. All the source code for this example can be found on GitHub. This example will show you how to use LaunchFlow to create a Cloud SQL database to hold user information, and a GCS storage bucket to hold profile pictures. Then we will use an async database connection to interact with the database.

Code Walkthrough

There are three main files associated with our application: is where we define the resource our application will use. In this case we define a Cloud SQL database and a GCS bucket. When you run launchflow create from the root directory of the project these resources will be created in the project and environment of your choice.

import launchflow as lf
# TODO(developer): Set these variables with your own values
db = lf.gcp.CloudSQLPostgres("launchflow-demo-db")
lf_bucket = lf.gcp.GCSBucket("launchflow-demo-bucket-1")

Running the application

To run this application make sure you update the launchflow.yaml to point to your own project and environment.

Install the dependencies

pip install -r requirements.txt


To get started run login to LaunchFlow:

launchflow login

Connect GCP

If you haven't connected your LaunchFlow account to GCP, you can do so by running:

launchflow connect --provider=gcp

Create Resources

NOTE: Before running make sure you update the bucket name in to be unique.

To create the resources needed to run the application, run:

launchflow create

This will prompt you to create a LaunchFlow project and environment to hold the resources. It takes GCP several minutes to provision your Cloud SQL database.

Run the application

uvicorn app.main:app

Once running you can visit http://localhost:8000/docs to see the API documentation, and send requests.