DEV Community

Cover image for Web API with python
Idris Fagbemi
Idris Fagbemi

Posted on

1 1

Web API with python

Using flask to create a rest api with python.

import json
from flask import Flask, request, jsonify


app = Flask(__name__)
data = json.load(open('stock.json'))

@app.route('/')
def index():
    return 'Stock API'

@app.route('/api/v1/info', methods=['GET'])
def api_home():
    return jsonify(data)


@app.route('/api/v1/stocks', methods=['GET'])
def api_stocks():
    if 'symbol' in request.args:
        symbol = request.args['symbol']
    else:
        return "Error: No name field provided. Please specify a name."

    data_list = data['quotedata']

    for i in data_list:
        # sym = i["symbol"]

        if i['symbol'] == symbol:
            details = {
                "name": i["longname"],
                "datatype": i["datatype"],
                "exchange": i["exchange"],
                "price_data": i["pricedata"]
            }

    return jsonify(details)  


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

Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay