Getting accurate, relevant and timely information is important for our world today. With the rise of sophisticated technology, there is the emergence of fake news and deep fakes, which may be very confusing and misleading.
Fortunately, today you can receive information on local and international news within your mobile app or desktop application using news APIs. Such is the Newscatcher API, which can use various programming languages such as Python, PHP, Ruby, and JavaScript.
What is Newscatcher API?
The Newscatcher API is a powerful application programming interface that allows users to find news articles by topic, language, country, website, and keywords.
The API assists developers in retrieving the latest headlines by topic or nation. Moreover, users can find out the number of times a given company, person, or event gets mentioned per hour or daily.
The API uses multiple languages to ensure that users get the most relevant results possible. Prior knowledge of a particular language helps significantly improve the API search engine’s quality and speed.
How It Works
The API works using simple logic. It receives requests from client devices and communicates with the server to process the client request. Once the server has executed the request, it returns the API results, which delivers them to the client device as a JSON file.
Users
Stock Traders: Information about price changes at stock exchange markets is crucial as it provides investment opportunities. Stock traders use the API to get news on the market trends and know high-performing companies so that they can invest or trade their stocks for profit.
Bloggers: Bloggers who make a living through writing about trending topics and explaining new information use the API to gather the latest news on particular subjects, people, or companies.
Researchers: Credible news is important for researchers, and thus this API comes in handy for them. The API allows researchers to find particular articles within a specified time period to back up their findings and research.
Companies: Organizations use the API to find out how their recently launched products are doing. Companies can assess their products ‘ performance by seeing the number of mentions in articles and gathering comments on the articles.
Additionally, enterprises can gauge themselves in the market against their competitors using this information. Finding news about their competition is important in adopting new strategies and technologies.
How to Connect to Newscatcher API Tutorial – Step by Step
Step 1. Sign Up for a RapidAPI Account to Get The API Key
RapidAPI is the world’s biggest API marketplace, with more than 20,000 APIs of different types and a community of millions of developers. The site aims to help developers find and connect to APIs to help them create useful and amazing applications.
To create a RapidAPI account, go to rapidapi.com and sign up using email, Google, Github, or Facebook.
Step 2.Subscribe to The Newscatcher API.
After you have successfully registered or logged on to your RapidAPI account, you need to subscribe to the API. To do this, navigate to the Newscatcher API and select it. Go to the Pricing page of this API and choose the plan you want. Luckily for us, we have a free plan for unlimited API calls. To select the Basic plan, click on the Select Plan button.
Step 3. Test the API Subscription
Now that you already have the plan you need, running a test for the endpoints is important. To do this, go back to the Endpoint tab. The Newscatcher API has seven endpoints, as displayed in the image below.
Test any endpoint and select the Code Snippet for nearly any programming language, including Python, JavaScript, PHP, and Ruby. RapidAPI will automatically paste your unique API Key.
Image displaying the available endpoints on Newscatcher API.
Upon a successful test of an endpoint, you should get a result with a status code of 200 OK, as shown below. A different result means the test has failed.
Explanation of Newscatcher Endpoints
As can be seen from the image above, the Newscatcher API has seven endpoints. Let us discuss further what each endpoint does.
The Sources Endpoint: This endpoint allows users to restrict the news articles to their chosen topic. The only acceptable values are news, sport, tech, world, finance, politics, business, economics, entertainment, beauty, travel, music, food, and science. The endpoint allows users to key in the source URL from websites like Reuters, New York Times, Market Watch, and The Guardian.
The Aggregation Endpoint: The endpoint combines articles from selected websites and returns articles count per hour or day. However, the API does not allow input of more than a hundred days or beyond a hundred hours. It also allows developers to include multiple sources or exclude a particular source. You may also limit the search results to the top one million websites and rank the sites in ascending or descending order of popularity.
The Stocks Endpoint: This endpoint gathers news articles for publicly traded companies. The parameters allowed here are tickers (also called stock symbols) traded on the NYSE, ASX, AMEX, NASDAQ, EURONEXT, LSE, SGX, TSX, TSXV, HKEX, and SIX. Additionally, it allows users to find high-ranking officials of a company such as CEO, founder, vice president, chief finance officer, and many more. The basic plan allows up to 100 articles per page when returning results, although, by default, it delivers 50 articles.
The Search Endpoint: The endpoint allows users to refine their search in a customizable way by allowing filters and sorting. You may tailor your search by relevancy, date, rank, time zones, topics, source, or media content.
The Search Free Endpoint: This endpoint allows users to search for free, allowing only the language filter. It produces up to a hundred articles for a single API call irrespective of the subscription plan.
The Latest Headlines Endpoint: The endpoint delivers the latest 100 articles based on topic, country or language, or any combination of those. However, note that the endpoint may fail to return some of the most recent articles if they do not contain a title.
The Search Enterprise Endpoint: This is the ultimate search endpoint for this API. It aids users in sorting and filtering their search by all available options. However, the users of the basic plan do not have access to this feature.
How to Use Newscatcher API with Python
Ensure you have installed the Python application on your computer to use this tutorial.
The code below is for the search endpoint. We search for Elon Musk using the English language for the most relevant articles with a media file on the first page.
Code Snippet:
import requests url = "https://newscatcher.p.rapidapi.com/v1/search" querystring = {"q":"Elon Musk","lang":"en","sort_by":"relevancy","page":"1","media":"True"} headers = { 'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 'x-rapidapi-host': "newscatcher.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
How to Use Newscatcher API with PHP
Before jumping into how the API works with PHP, it is paramount to ensure you have PHP installed on your computer. If you do not have PHP on your computer, please consider following an installation guide. Now that we already have the application installed in our system, let us check sample codes for it.
Here is a code sample in PHP for the aggregation endpoint.
$curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://newscatcher.p.rapidapi.com/v1/aggregation?q=Apple&agg_by=day&media=True", CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => [ "x-rapidapi-host: newscatcher.p.rapidapi.com", "x-rapidapi-key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; }
The above code aggregates articles containing Apple for 1st June 2020. After running, the code output for the PHP code is displayed.
How to Use Newscatcher API with Ruby
Ruby is a powerful programming language that we can use in developing amazing apps. Suppose you do not have Ruby installed on your computer; follow installation guides for further information.
After you have Ruby installed, you need to create a client for easy interaction with the API. This action helps to avoid duplication of codes. To achieve this, create a new folder in the library folder called Newscatcher and a new file called client.rb. Here is the code to initiate this interaction:
module Newscatcher class Client def initialize @host = 'YOUR_HOST' @key = 'YOUR_API_KEY' end def item(id) get("item/#{id}") end def topstories get('topstories') end private def get(path) response = Excon.get( 'https://' + @host + '/' + path + '.json?print=pretty', headers: { 'x-rapidapi-host' => @host, 'x-rapidapi-key' => @key, } ) return false if response.status != 200 JSON.parse(response.body) end end end
Enter the API key from RapidAPI to connect to the API. Once you have completed the connection, you may proceed to see how the endpoints work.
The code snippet below using Ruby shows an API call to retrieve the latest article headlines in Australia about tech.
Example of Code Snippet for Ruby
require 'uri' require 'net/http' require 'openssl' url = URI("https://newscatcher.p.rapidapi.com/v1/latest_headlines?topic=tech&lang=en&country=Australia&media=True") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-rapidapi-key"] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' request["x-rapidapi-host"] = 'newscatcher.p.rapidapi.com' response = http.request(request) puts response.read_body
How to Use Newscatcher API with JavaScript
To use the API in JavaScript, users need to first set up the JavaScript environment. To achieve this, you need to download text editors such as Atom. Alternatively, you can download and install it from the official website.
Example of a code Snippet on JavaScript for tech search in the US in English language using the sources endpoint.
const request = require('request'); const options = { method: 'GET', url: 'https://newscatcher.p.rapidapi.com/v1/sources', qs: {topic: 'tech', lang: 'en', country: 'USA'}, headers: { 'x-rapidapi-key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'x-rapidapi-host': 'newscatcher.p.rapidapi.com', useQueryString: true } }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); });
The x’s represent your API key on RapidAPI, so your key will appear here.
Sample response for the code above
Benefits of Newscatcher API
Variety: The API allows users to search for multiple sources. It is easy to find articles from many sources with a single call about a given topic, using a preferred language and based in a particular country.
The API supports ten different languages, including Dutch, French, German, Italian, Spanish, and Arabic. Having multiple languages allows users to customize and find particular news with pinpoint accuracy and relevancy. Also, the auto-detection of languages used in articles allows easy analysis according to the language specifications.
Speed: Getting all the latest headlines from any website on any topic may be cumbersome and mind-boggling. However, this news API lifts off this burden and produces desired results within a short period. Moreover, the information delivered is in real-time.
Customization: The API allows users to customize their search using several parameters. Having many options for filtering and sorting leads to narrowed search results. Therefore, users can rely on the API to retrieve articles of greater relevance and interest.
Hand-picked Sources: The API offers suggestions to get articles from the world’s top-rated websites. Moreover, users can easily add or remove any site as they wish. Such autonomy ensures users get quality news updates from the market leaders.
Alternatives to Newscatcher API
There are several other news APIs available at RapidAPI. Such include the following;
Hacker News API: This API is best for capturing the latest news in the world of technology.
Connexion News API: It is best for historical and trending content from trusted sources around the world.
Bing News Search API: The API helps users discover the latest and trending news based on a particular keyword search.
MyAllies Breaking News API: The API is best for the latest news in business and stock pricing worldwide.
Financial Times API: The API allows users to get content on the latest from Financial Times publications.
Newslit API: This API is best for news monitoring of high-quality news sources.
Summary
The Newscatcher API is a powerful tool for getting credible and reliable news. Different developers are more conversant with particular coding languages over others. Therefore, this guide will help in the easy integration of the API in their apps.
Leave a Reply