Book Image

Python API Development Fundamentals

By : Jack Chan, Ray Chung, Jack Huang
Book Image

Python API Development Fundamentals

By: Jack Chan, Ray Chung, Jack Huang

Overview of this book

Python is a flexible language that can be used for much more than just script development. By knowing the Python RESTful APIs work, you can build a powerful backend for web applications and mobile applications using Python. You'll take your first steps by building a simple API and learning how the frontend web interface can communicate with the backend. You'll also learn how to serialize and deserialize objects using the marshmallow library. Then, you'll learn how to authenticate and authorize users using Flask-JWT. You'll also learn how to enhance your APIs by adding useful features, such as email, image upload, searching, and pagination. You'll wrap up the whole book by deploying your APIs to the cloud. By the end of this book, you'll have the confidence and skill to leverage the power of RESTful APIs and Python to build efficient web applications.
Table of Contents (12 chapters)

Setting Up Environment Variables for the Heroku App

We previously modified config.py and added ProductionConfig there. Now we have to add the environment variables in Heroku, including the secret key and database URL. In addition to these, let's not forget the Mailgun API key and the Mailgun domain as well. We will set up all of these together in the next exercise.

Exercise 67: Setting Up the App Environment Variables

In this exercise, we will set up the environment variables in the production environment. Fortunately, because we are using Heroku Postgres, the database URL environment variable has already been set up for us. We only need to set ENV, SECRET_KEY, MAILGUN KEY, and DOMAIN. Then, once the setting is complete, after the Deploy code is completed, the application will read the newly added environment variables in App config:

  1. Generate the secret key using the following two lines of code in the Python console in PyCharm:
    >>>import os
    >>>os...