DEV Community

Vivesh
Vivesh

Posted on

4

Application Programming Interface

What is an API?

API (Application Programming Interface) is a set of rules and protocols that allows different software applications to communicate with each other. It serves as an intermediary between applications, enabling them to exchange data and perform functions. APIs are widely used for integrating systems, enabling third-party developers to interact with a service, and exposing functionality in a standardized way.

API Management

API Management involves the creation, publication, monitoring, and management of APIs in a secure and scalable manner. It ensures APIs are reliable, available, and secure. Key aspects include:

  • Authentication and Authorization: Ensuring only authorized users can access the API.
  • Rate Limiting: Restricting the number of requests to prevent misuse.
  • Analytics and Monitoring: Tracking usage metrics, performance, and errors.
  • Documentation: Providing developers with clear guidance on how to use the API.
  • Versioning: Managing different versions of an API to ensure backward compatibility.

Task: Create a Simple REST API using Flask and Deploy it on AWS

Step 1: Set up Flask and Create the API

  1. Install Flask:
   pip install flask
Enter fullscreen mode Exit fullscreen mode
  1. Write a Simple REST API: Create a file named app.py:
   from flask import Flask, jsonify, request

   app = Flask(__name__)

   # Sample data
   users = [
       {"id": 1, "name": "John Doe"},
       {"id": 2, "name": "Jane Doe"}
   ]

   # Get all users
   @app.route('/users', methods=['GET'])
   def get_users():
       return jsonify(users)

   # Get a single user by ID
   @app.route('/users/<int:user_id>', methods=['GET'])
   def get_user(user_id):
       user = next((user for user in users if user["id"] == user_id), None)
       return jsonify(user) if user else ("User not found", 404)

   # Add a new user
   @app.route('/users', methods=['POST'])
   def add_user():
       new_user = request.get_json()
       users.append(new_user)
       return jsonify(new_user), 201

   if __name__ == '__main__':
       app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode
  1. Test the API Locally: Run the API:
   python app.py
Enter fullscreen mode Exit fullscreen mode

Access it at http://127.0.0.1:5000/users.

Step 2: Prepare for AWS Deployment

  1. Install gunicorn (for production-grade deployment):
   pip install gunicorn
Enter fullscreen mode Exit fullscreen mode
  1. Create a requirements.txt: Generate a list of dependencies:
   pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. Add a WSGI Entry Point: Create a file wsgi.py:
   from app import app

   if __name__ == "__main__":
       app.run()
Enter fullscreen mode Exit fullscreen mode

Step 3: Deploy on AWS

  1. Launch an EC2 Instance:

    • Select an Amazon Linux or Ubuntu instance.
    • Open ports 22 (SSH) and 5000 (or your desired API port) in the security group.
  2. Install Required Packages on EC2:

   sudo yum update -y  # or apt-get update for Ubuntu
   sudo yum install python3-pip -y
Enter fullscreen mode Exit fullscreen mode
  1. Upload Your Application: SCP or use AWS CLI to transfer files to the instance:
   scp -i your-key.pem app.py requirements.txt wsgi.py ec2-user@<EC2-IP>:~/
Enter fullscreen mode Exit fullscreen mode
  1. Install Dependencies:
   pip3 install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. Run the API Using Gunicorn:
   gunicorn --bind 0.0.0.0:5000 wsgi:app
Enter fullscreen mode Exit fullscreen mode
  1. Access Your API: Visit http://<EC2-Public-IP>:5000/users.

Optional: Use AWS Elastic Beanstalk

To automate deployment, you can package your Flask application and deploy it using AWS Elastic Beanstalk for a fully managed environment.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️