title: Falcon API on App Engine Standard Environment description: Learn how to build a Falcon API in the Google App Engine standard environment. author: archelogos tags: App Engine, Python, Falcon, API date_published: 2017-04-27
This tutorial shows how to build a Python API with Falcon.
Falcon is a high-performance Python framework for building cloud APIs. It follows the REST architectural style, and tries to do as little as possible while remaining highly effective.
In order to follow this guide, you will need to install Python in your local machine.
- Create a Python app that uses Falcon as a framework.
- Run the app locally.
- Deploy the Python app to Google App Engine standard environment.
This tutorial does not use billable components of Google Cloud Platform so you do not need to enable the billing for your project to complete this tutorial.
Before you begin
- Create a project in the Google Cloud Platform Console and make note of the project ID.
- Install the Google Cloud SDK
Preparing the app
requirements.txtfile with the following contents:
appengine_config.pyfile with the following contents:
py from google.appengine.ext import vendor vendor.add('lib')
app.yamlfile with the following contents:
```yaml runtime: python27 api_version: 1 threadsafe: true
handlers: - url: /.* script: api.app ```
apimodule in your workspace
This module contains the following files:
__init__.py. This is where the api module is initialized and its routes are created. You can see how the app variable is defined using the Falcon library.
py app = falcon.API(middleware=[ AuthMiddleware() ])
You can add a route with the following method:
py app.add_route('/', Resource())
- The resources can be defined in the
Resourceclass implements four different methods
on_deletethat define the endpoints for each HTTP method.
```py def on_get(self, req, resp): ...
def on_post(self, req, resp): ...
def on_patch(self, req, resp): ...
def on_delete(self, req, resp): ... ```
- In the
middleware.pyfile you can find the
AuthMiddlewareclass which is used to ensure that all requests are authenticated. Because this is just an example, it is not implemented with any kind of validation.
```py class AuthMiddleware(object):
def process_request(self, req, resp): token = req.get_header('Authorization') ...
hooks.pyfile contains definitions of custom functions that can be called before or after an endpoint function is executed. They can be used, for instance, to validate the input data or serialize the API responses.
Running the app
Install the dependencies into the
libfolder with pip.
pip install -t lib -r requirements.txt
Execute the following command to run the app.
Visit http://localhost:8080 to see the app running.
Run the following command to test one of the endpoints:
curl -X GET \ http://localhost:8080/ \ -H 'authorization: Bearer 1234' \ -H 'cache-control: no-cache'
Deploying the app
Run the following command to deploy your app:
gcloud app deploy
http://[YOUR_PROJECT_ID].appspot.comto see the deployed app.
[YOUR_PROJECT_ID]with your Google Cloud Platform project ID.
Run the following command to view your app:
gcloud app browse