Simple Event Registration API with MongoDB and Flask
Tags: pymongo, mongodb, flask, python
Published: 2019-11-28 06:30:17

---
title: Simple Event Registration API with MongoDB and Flask
published: true
description: null
tags: null
---
# let's start

Simple Event Registration API is a demonstration of how to create, read and write data with MongoDB using Flask 

# MongoDB
MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. Its document model is simple for developers to learn and use, while still providing all the capabilities needed to meet the most complex requirements at any scale but You can [get started here](https://docs.mongodb.com/guides/)
# Use cases for MongoDB include the following;
1. [ Single View ](https://www.mongodb.com/use-cases/single-view)
2. [ The Internet Of Things ](https://www.mongodb.com/use-cases/internet-of-things)
3. [ Mobile ](https://www.mongodb.com/use-cases/mobile)
4. [ Real-Time Analytics ](https://www.mongodb.com/use-cases/real-time-analytics)
5. [ Personalization ](https://www.mongodb.com/use-cases/personalization)
6. [ Catalog ](https://www.mongodb.com/use-cases/catalog)
7. [ Content Management ](https://www.mongodb.com/use-cases/content-management)

> To install MongoDB [Click here](https://docs.mongodb.com/guides/server/install/) if not installed 


# [ Flask ](https://flask.palletsprojects.com/en/1.1.x/)
- Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.

If you are new to python and Flask You  can get started here with the following articles 
first, get to understand [ python ](https://www.geeksforgeeks.org/python-language-introduction/) secondly start playing with [ Flask ](https://www.geeksforgeeks.org/python-build-a-rest-api-using-flask/)

# [ Postman ](https://www.guru99.com/postman-tutorial.html)
This is a tool that makes the game of building API's fun,  its a must-have before even thinking of building an API. Postman is currently one of the most popular tools used in API testing. It started in 2012 as a side project by Abhinav Asthana to simplify API workflow in testing and development. 
> API stands for Application Programming Interface which allows software applications to communicate with each other via API calls.

More about APIs [Click Here](https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask)

# Set up
> Install python on your machine if not installed you can check by using the following command `python3 --version` 
> check if the virtual environment is installed if not install it  [ Linux ](https://gist.github.com/Geoyi/d9fab4f609e9f75941946be45000632b) for [Windows](https://programwithus.com/learn-to-code/Pip-and-virtualenv-on-Windows/)

#### 1.Create Virtual environment 
` python3 -m venv venv `

> venv - means the name of the virtual environment it can be anything
> [Read More](https://www.bogotobogo.com/python/python_virtualenv_virtualenvwrapper.php)

#### 2.Activate Virtual environment 
`  source venv/bin/activate ` or ` . venv/bin/activate `

#### 2.Installing the following

1. Flask 
` pip install flask`

2. Pymongo 
`pip install pymongo`

3. check using ` pip list `

- Create a file called app.py with the following command
` Touch app.py`
- paste the following code
 
``` 
from flask import Flask, jsonify, request
from flask_pymongo import PyMongo

app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://localhost:27017/regdb"
mongo = PyMongo(app)


@app.route('/', methods = ['GET'])
def get_all_attendees():
"""To get all the items from the database.its a Get request and we use the databasename.find()"""
	attendees = mongo.db.attendees
	output = []
	
	for r in attendees.find():
		output.append({
                    'name':r['name'],
		    'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
	return jsonify({'result' : output})

@app.route('/attendee/<name>', methods = ['GET'])
def get_one_attendee(name):
"""To get one item from the database.its a Get request and We use the databasename.find_one"""
	attendees = mongo.db.attendees
	attendees.find_one({'name':name})

	output.append({
                    'name':r['name'],
		    'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
	return jsonify({'result' : output})

@app.route('/register', methods = ['POST'])
def register():
"""To add an item to the database. its a post request and we use databasename.insert()"""
	attendees = mongo.db.attendees
	name = request.json['name']
	language=request.json['language']
	reg_id = attendees.insert({
                    'name':r['name'],
		    'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
	r = attendees.find_one({'_id':reg_id})
        output = ({
                    'name':r['name'],
		    'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
        return jsonify({'result' : output})

if __name__== '__main__':
	app.run(debug=True)
```



Go ahead and test your API with Postman

Thanks, Ernest