Access Top Headlines

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.

Press + to interact

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

apiKey

String

Mandatory

This is the API key.

country

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 source parameter.

category

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 source parameter.

sources

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 country or category parameter.

q

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 everything endpoint.

pageSize

Integer


Optional

This is the number of results to return per page. The default value is 20, and 100 is the maximum.

page

Integer

Optional

This is used to get a specific page in the results. It is 1 by default.

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.

Press + to interact
// Define endpoint URL here
const endpointUrl = new URL("https://newsapi.org/v2/top-headlines");
const headerParameters = {
contentType: "application/json",
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
apiKey: '{{API_KEY}}',
country: 'us'
});
// Setting API call options
const options = {
method: "GET",
headers: headerParameters,
};
// Function to make API call
async function searchHeadlines() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
searchHeadlines();

In the above code widget:

  • Line 2: We assign the endpoint URL to the endpointUrl variable.

  • Lines 9–12: We add apiKey and country in the queryParameters variable.

  • Line 24: We pass endpointUrl and options as input parameters to the fetch function and use it to make an API call.

Response fields

Some important response fields are listed in the table below:

Name

Type

Description

status

String

This is the status of the request. If the request is successful, its value is ok. Otherwise, it’s error. In case of an unsuccessful request, code and message are also populated.

totalResults

Integer

This is the total number of articles available for your query. Only a limited number of articles are shown depending on the pageSize parameter. The remaining articles can be viewed by calling the API with page parameter equal to the page number.

articles

Object[array]

This array contains information about the retrieved articles.

source

Object

This object contains the name and id of the source which published the article.

author

String

This is the name of the author of the article.

title

String

This is the title of the article.

description

String

This is the description or a snippet of the article.

url

String

This is the direct URL of the article.

urlToImage

String

This is the URL to the relevant image of the article.

publishedAt

String

This is the date and time that the article was published in UTC (+000)

content

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.

Press + to interact
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 510: The queryParameters contains the following request parameters:

    • The country is set to us, which is the 2-letter ISO 3166-1 code of the United States.

    • The category parameter is assigned technology.

    • 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.

Press + to interact
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 assigned Tesla.

    • pageSize is set to 100, 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.