Photo by Ciprian Boiciuc on Unsplash

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

Joel Hanson
3 min readApr 14, 2019

--

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.

Let's build an API!!

Goals:

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

Install Flask and Faker

pip install flask
pip install Faker

Check if the flask is installed and working fine, make a file called hello.py with the following code.

# hello.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

And run the following code in the path where the file is created.

$ FLASK_APP=hello.py flask run

You will get the response

* Running on http://localhost:5000/

Then you can confirm that your application is running checking the URL http://localhost:5000/ in your browser and can see Hello World.

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.

  1. Create a new folder called flask_api
$ mkdir flask_api

2. Create a new file called server.py within the flask_api folder

$ # Make a server file
$ cd flask_api
$ touch server.py

3. Copy paste the following code to server.py.

# Import nessecary parts from flask and faker to generate random    # name and email.
from
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 = fake.name()
email = fake.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.
if
__name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

Running the server

  1. Save your server.py file
  2. In a terminal navigate to your flask_api folder
  3. Enter command python server.py to run server
  4. Navigate to http://localhost:5000 in your web browser
  5. You are now running your own server locally!

you can find the random name and email JSON response in the browser on navigating to http://localhost:5000 example:

{
"email": "douglasavery@mcgee.org",
"name": "Anthony Rogers"
}

The source code for the current application is provided in my GitHub.

https://github.com/Joel-hanson/flask_api

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.

Next things we are going to do:

  1. Get a Heroku account.
  2. Add your code to GitHub.
  3. Deploy your application to Heroku.

Please provide your feedback and suggestions. Follow me to get the latest updates.

Linkedin: linkedin.com/in/joel-hanson/

Portfolio: joel-hanson.github.io/

Github: github.com/Joel-hanson

Twitter: twitter.com/Joelhanson25

--

--