![Barandev](https://storage.googleapis.com/lazy-apps-images-bucket/authors/99e0898ee613669bd058e22583481df2_66223175657b8290dea7cfb6_T01K7EFD532-U06N4U2F6JZ-5a00594acfba-512.jpg)
by Barandev
News API
import os
import logging
from flask import Flask, request, jsonify, render_template
import requests
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
NEWS_API_KEY = os.environ['NEWS_API_KEY']
NEWS_API_URL = "https://newsapi.org/v2/top-headlines"
@app.route("/news", methods=["GET"])
def get_news():
categories = request.args.getlist('category')
country = request.args.get('country', default='us', type=str)
all_headlines = []
try:
for category in categories:
headlines = fetch_headlines(category.capitalize(), country)
all_headlines.extend(headlines)
return jsonify(all_headlines), 200
except requests.exceptions.RequestException as e:
News API
Created: | Last Updated:
Introduction to the News API App Template
Welcome to the News API App template! This template is designed to help you create a web application that fetches and displays news headlines based on user-selected categories. It uses Flask, a lightweight web application framework in Python, to serve web pages and handle requests. The front end is built with HTML, CSS, and JavaScript, allowing for an interactive user experience. This step-by-step guide will walk you through the process of setting up and using this template on the Lazy platform.
Getting Started
To begin using this template, click on "Start with this Template" in the Lazy Builder interface. This will pre-populate the code in the Lazy Builder, so you won't need to copy or paste any code manually.
Initial Setup: Adding Environment Secrets
Before you can use the News API App, you'll need to set up an environment secret for the NEWS_API_KEY. This key is required to authenticate requests to the News API service. Here's how to obtain and set up your NEWS_API_KEY:
- Visit https://newsapi.org/ and sign up for an API key.
- Once you have your API key, go to the Environment Secrets tab within the Lazy Builder.
- Create a new secret with the key as NEWS_API_KEY and paste your API key as the value.
This API key will be used by the application to fetch news headlines from the News API service.
Test: Deploying the App
With the NEWS_API_KEY set up, you're ready to deploy the app. Press the "Test" button in the Lazy Builder. This will start the deployment process and launch the Lazy CLI. The app will be deployed on the Lazy platform, and you won't need to worry about installing libraries or setting up the environment.
Using the App
Once the app is deployed, Lazy will provide you with a dedicated server link. Use this link to access the web interface of your News API App. Here's how to interact with the app:
- Open the provided server link in your web browser.
- You will see a form with checkboxes for different news categories such as General, Business, Entertainment, etc.
- Select the categories you're interested in and click the "Get News" button.
- The app will display a list of the top 10 headlines for each selected category.
The front end will make requests to the Flask backend, which in turn fetches news from the News API and returns it to the front end for display.
Integrating the App
If you wish to integrate this News API App into another service or front end, you can use the server link provided by Lazy as the base URL for your API requests. Here's a sample request you might make to the app's API:
GET /news?category=general&category=business
And here's a sample response you might receive:
[
{
"title": "Breaking News: Market sees unprecedented growth",
"source": "Financial Times",
"publishedAt": "2023-04-01T12:00:00Z",
"category": "Business"
},
// More articles...
]
Use this API to fetch news headlines and integrate them into your own applications or services.
By following these steps, you should now have a fully functional News API App running on the Lazy platform. Enjoy delivering the latest news to your users!
Technologies
![Streamlit](https://raw.githubusercontent.com/github/explore/968d1eb8fb6b704c6be917f0000283face4f33ee/topics/streamlit/streamlit.png)
![Python](https://i.pinimg.com/originals/2f/9c/11/2f9c11f9e55efbf1791f12c06d60729b.jpg)