Photo by Ciprian Boiciuc on Unsplash

How to make a simple API for generating random names and emails using Flask.

This article is for the beginner who is looking to create an API using the flask. We will be creating an API that can return a random name and email for every request that reaches the API.


  1. Build the application.
  2. Deploying the application.

Install Flask and Faker

pip install flask
pip install Faker
from flask import Flask
app = Flask(__name__)

def hello():
return "Hello World!"
$ flask run
* Running on http://localhost:5000/

Part 1. Building the application

To begin, we are going to be using the command line to build out a folder and make files for our application.

$ mkdir flask_api
$ # Make a server file
$ cd flask_api
$ touch
# Import nessecary parts from flask and faker to generate random    # name and email.
flask import Flask, request, jsonify
from faker import Faker
# To create and initialize a faker generator.
fake = Faker()
# Create the app object that will route our calls.
app = Flask(__name__)
# Add a single endpoint that we can use as an API to accept GET and # POST requests.
@app.route("/", methods=["POST", "GET"])
def index():
# fake to create random name and email
name =
email =
response = {
"name": name,
"email": email
# return name and email as a JSON httpresponse using jsonify
return jsonify(response)
# When run from command line, start the server.
__name__ == '__main__':, host='')
  1. In a terminal navigate to your flask_api folder
  2. Enter command python to run server
  3. Navigate to http://localhost:5000 in your web browser
  4. You are now running your own server locally!
"email": "",
"name": "Anthony Rogers"

Next Steps

We have done the coding part for our application. The continuation parts of this post will be showing you how to deploy this application.

  1. Add your code to GitHub.
  2. Deploy your application to Heroku.

Works at @impressAI