How to use News API with examples
What is the purpose of News api?
Before we jump into this it is worth understanding what the News api can do for us. Have you ever used Google News? Now imagine that you can search it’s history based on keywords, phrases or find trending topics in the last 5 years of Google News. Quite powerful isn’t it?
What you do with that information is up to you, but just to give you a few ideas:
- find live events and news that affect your business or clients
- find trends over the past years to try to predict what type of events might be happening in the next years
Get the API key
First start with getting your API key from the Newsapi registration. You will have to enter your email and create an account. You can not get more than 1 API. The key is free and unlimited for development purposes. Once your app goes live you would need to get one of the paid plans.
Build the request
In this article we will create a few simple requests to test the api. We will be using a simple PHP script to build the url, pass the necessary headers and parameters via curl.
Make sure you have curl installed. If you do not simply use your package manager to install the package, for ubuntu it would be something like apt install php7.4-curl.
First things first, we will need to endpoint. Since I want to start small, I will be using the everything endpoint - https://newsapi.org/docs/endpoints/everything . The important parameters that we need to pass are q - the search phrase we are looking for. Then we can use others to limit the search range by either language, dates, relevancy, and also sort and paginate the results.
The only required parameter is the API key. ou can pass as either a GET parameter or a header. It would be better to use a header because GET parameters would show up in the request url, logs and anyone could intercept it fairly easy.
Here is the final script.
<?php
$query = 'bee gee';
$curl = curl_init();
$parameters = array(
'q' => $query,
'sortBy' => 'publishedAt',
);
$apiUrl = '<https://newsapi.org/v2/everything>' . '?' . http_build_query($parameters);
curl_setopt($curl, CURLOPT_URL, $apiUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: '.$apiKey, // pass your key here
'User-Agent: testing'
));
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
User agent
{"status":"error","code":"userAgentMissing","message":"Please set your User-Agent header to identify your application. Anonymous requests are not allowed.”
You will notice that in headers I included User Agent. It is because the API does not allow making requests without user agent. And since I was running this script from a terminal it means that there was no user agent assigned. Luckily you can pass anything and it allows it. It is unclear why this requirement is there, because you can easily rotate your user agent. Also if they wanted to block you they could simply disable your API key which I assume can also be easily used to identify the requests.
Top headlines
Next lets take a look at the top headlines. We will use the top headlines request - https://newsapi.org/docs/endpoints/top-headlines.
At the time of writing iphone 15 is about to come out and there is a lot of speculation about it’s usb charging. Let’s see if we can find some articles about this. To limit the search let’s look for only US articles.
$curl = curl_init();
$parameters = array(
'category' => 'technology',
'q' => 'iphone 15',
'country' => 'us'
);
$apiUrl = '<https://newsapi.org/v2/top-headlines>' . '?' . http_build_query($parameters);
curl_setopt($curl, CURLOPT_URL, $apiUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: '.$apiKey,
'User-Agent: testing'
));
$result = curl_exec($curl);
curl_close($curl);
Sure enough there are 5 results found and some funny ones as well. Here are the results:
{"status":"ok","totalResults":5,"articles":[{"source":{"id":null,"name":"MacRumors"},"author":"Tim Hardwick","title":"Google Mocks iPhone 15 and Late USB-C Adoption in Latest Ad - MacRumors","description":"Google has expanded on its #BestPhonesForever ad campaign with a new video titled \\"Spa Day\\" in which it pokes fun at the upcoming iPhone...","url":"<https://www.macrumors.com/2023/09/05/google-pokes-fun-iphone-15-usb-c-adoption/","urlToImage":"https://images.macrumors.com/t/_yHea7LfMGuB5hcXsO7KKuWlFsA=/1920x/article-new/2023/09/google-iphone-usb-c-ad.jpg","publishedAt":"2023-09-05T12:30:00Z","content":"Google> has expanded on its #BestPhonesForever ad campaign with a new video titled \\"Spa Day\\" in which it pokes fun at the upcoming iPhone 15's implied lack of headline features and its expected adopti… [+1906 chars]"},{"source":{"id":null,"name":"AppleInsider"},"author":"William Gallagher","title":"Action button shown in new iPhone 15 Pro case leak - AppleInsider","description":"A new video purporting to show cases for the iPhone 15 Pro also clearly shows how the old mute switch is replaced by an Action button.","url":"<https://appleinsider.com/articles/23/09/05/action-button-shown-in-new-iphone-15-pro-case-leak","urlToImage":"https://photos5.appleinsider.com/gallery/56160-114000-000-lead-Action-Button-xl.jpg","publishedAt":"2023-09-05T10:41:33Z","content":"The> Action button is on the side at the top, where the current mute switch is\\r\\nA new video purporting to show cases for the iPhone 15 Pro also clearly shows how the old mute switch is replaced by an … [+1149 chars]"},{"source":{"id":null,"name":"Komando"},"author":"Kim Komando","title":"iPhone 15, MagSafe upgrades, Apple Watch: Everything we're expecting from Apple's next reveal - Komando","description":"Want to know what Apple is going to announce at its Wonderlust event? Here is everything we expect to be on the way.","url":"<https://www.komando.com/gadgets/apple-wonderlust-event/893245/","urlToImage":"https://www.komando.com/wp-content/uploads/2023/09/kk-article-20230905-apple-wanderlust-event-iphone-15-more-1200x675-1.jpg","publishedAt":"2023-09-05T00:14:39Z","content":"The> leaves change colors, you grab a scarf, pumpkin spice is back and Apple has a big announcement all classic signs of fall. Sept. 12 is Wonderlust yeah, Apples naming them now. I know. \\r\\nFear not i… [+2790 chars]"},{"source":{"id":null,"name":"Android Police"},"author":"Chandraveer Mathur","title":"Apple may never acknowledge the EU's role in bringing USB-C to the iPhone 15 - Android Police","description":"Spinning the battle as the ultimate pro-consumer move","url":"<https://www.androidpolice.com/apple-may-never-admit-eu-role-usb-c-iphone-15/","urlToImage":"https://static1.anpoimages.com/wordpress/wp-content/uploads/2020/12/09/iPhone-12-Mini-vs-Pixel-5-3-scaled.jpg","publishedAt":"2023-09-04T19:15:00Z","content":"Summary\\r\\n><ul><li> The European Union has successfully forced Apple and other brands to adopt USB-C by the end of 2024, leading to the iPhone 15 series likely adopting sharing a port with Android devi… [+3981 chars]"},{"source":{"id":null,"name":"Forbes"},"author":"Gordon Kelly","title":"New iPhone 15 Exclusive Exposes Surprise Apple Design Decision - Forbes","description":"iPhone 15 buyers will not get one massive upgrade they were expecting...","url":"<https://www.forbes.com/sites/gordonkelly/2023/09/04/apple-iphone-15-pro-max-battery-life-a17-performance-new-iphone-release/","urlToImage":"https://imageio.forbes.com/specials-images/imageserve/64d0d8a6e2767e5aef16bfb6/0x0.jpg?format=jpg&crop=800,449,x0,y0,safe&width=1200","publishedAt":"2023-09-04T14:09:46Z","content":"Apple's> iPhone 15 Pro bezel Vs iPhone 14 Pro \\r\\nyeux1122\\r\\niPhone 15 leaks have exposed almost every detail about Apples new iPhones, but now one more surprising detail has now been revealed.\\r\\nIn a new… [+2137 chars]"}]}
With this information we can easily follow important events and build applications that can alert us when something happens. A simple idea would be to create a platform that you can configure your search terms and frequency, then the application would periodically check for any news in your region and alert you in case there are any headlines that you are looking for. I am sure there already are such applications but you could have this one for virtually free in a rather short time.