Access Top Headlines
Learn how to get headlines-related articles using News API.
News is a source of knowledge and reading the news helps you stay up-to-date on current affairs. But there is so much going on in the world that it is impossible for us to keep up with everything. Similarly, not every news article and blog can cover everything.
The Top headlines endpoint
The Top headlines endpoint provides headlines from different news sources. We can filter these headlines by using different query parameters to minimize the number of articles that are returned in the response of a successful API call.
The base URL of this endpoint is https://newsapi.org/v2/top-headlines
.
Request parameters
The Top headlines endpoint uses the following query parameters:
Parameters | Type | Category | Description |
| String | Mandatory | This is the API key. |
| String | Optional | This refers to the key of the country that the user wants to get headlines for. The possible options are listed in the appendix. This parameter cannot be used with the |
| String | Optional | This refers to the category that the user wants to get headlines—for example, sports or technology. This parameter cannot be used with the |
| String | Optional | These are sources we want headlines from. Multiple sources are separated using commas. We will get these sources using the Sources endpoint. This parameter cannot be used with the |
| String | Optional | These are the keywords to look for in the title and body of the article. It supports advanced search just like for the |
| Integer | Optional | This is the number of results to return per page. The default value is 20, and 100 is the maximum. |
| Integer | Optional | This is used to get a specific page in the results. It is |
We are using the code widget below to make a call to the Top headlines endpoint. Click the "Run" button below to see the response.
// Define endpoint URL hereconst endpointUrl = new URL("https://newsapi.org/v2/top-headlines");const headerParameters = {contentType: "application/json",};// Define Query Parameters hereconst queryParameters = new URLSearchParams({apiKey: '{{API_KEY}}',country: 'us'});// Setting API call optionsconst options = {method: "GET",headers: headerParameters,};// Function to make API callasync function searchHeadlines() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callsearchHeadlines();
In the above code widget:
Line 2: We assign the endpoint URL to the
endpointUrl
variable.Lines 9–12: We add
apiKey
andcountry
in thequeryParameters
variable.Line 24: We pass
endpointUrl
andoptions
as input parameters to thefetch
function and use it to make an API call.
Response fields
Some important response fields are listed in the table below:
Name | Type | Description |
| String | This is the status of the request. If the request is successful, its value is |
| Integer | This is the total number of articles available for your query. Only a limited number of articles are shown depending on the |
| Object[array] | This array contains information about the retrieved articles. |
| Object | This object contains the |
| String | This is the name of the author of the article. |
| String | This is the title of the article. |
| String | This is the description or a snippet of the article. |
| String | This is the direct URL of the article. |
| String | This is the URL to the relevant image of the article. |
| String | This is the date and time that the article was published in UTC (+000) |
| String | This contains unformatted content of the article where content is available. The content is truncated to 200 chars. |
Examples
Let’s look at a few use cases of the Top headlines endpoint.
Top five technology headlines from the United States
There is always something happening in the technology sector and the United States is one of the world's technological epicenters. So, let's look at how to find the top five technology headlines from the United States by using the Top headlines endpoint. Click the "Run" button below to see the response.
const API_KEY = '{{API_KEY}}';const endpointUrl = new URL("https://newsapi.org/v2/top-headlines");const queryParameters = new URLSearchParams({apiKey: API_KEY,country: 'us',category: 'technology',pageSize: 5});const headerParameters = {contentType: "application/json",};const options = {method: 'GET',headers: headerParameters};async function fetchNews() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);}catch (error) {printError(error);}}fetchNews();
In the above code widget:
Line 3: The
endpointUrl
variable contains the Top headlines endpoint URL.Lines 5–10: The
queryParameters
contains the following request parameters:The
country
is set tous
, which is the 2-letter ISO 3166-1 code of the United States.The
category
parameter is assignedtechnology
.The page size is set to five using
pageSize=5
.
Line 24: We use the
fetch
function to make an API call.
Top headlines about Tesla
With its electric vehicles, solar panels, and integrated renewable energy solutions for houses, Tesla has played a vital role in quickening the global shift to sustainable energy. Let's look at the top headlines related to Tesla. Click the "Run" button below to see the response.
const API_KEY = '{{API_KEY}}';const endpointUrl = new URL("https://newsapi.org/v2/top-headlines");const queryParameters = new URLSearchParams({apiKey: API_KEY,q: 'tesla',pageSize: 100,});const headerParameters = {contentType: "application/json",};const options = {method: 'GET',headers: headerParameters};async function fetchNews() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);}catch (error) {printError(error);}}fetchNews();
In the above code widget:
Line 3: The
endpointUrl
variable contains the API endpoint to get news headlines.Lines 5–9: The
queryParameters
variable consists of the tailored request parameters to achieve the desired result:q
is assignedTesla
.pageSize
is set to100
, which is the maximum number of headlines that can be called in one API call.
Line 23: We use the
fetch
function to make the API call.