The sources Endpoint
Learn about the back-end endpoint and how to use it.
We'll cover the following...
The sources endpoint is used at the backend. It provides sources that can be used as query parameters with the front-end endpoints. The base URI for this endpoint is https://newsapi.org/v2/top-headlines/sources.
Query parameters
Parameter | Type | Description |
| string | This helps us find sources that display news of a specific category. By default, the user will get sources for all categories in the result. |
| string | This is used to find sources that display news in a specific language. The possible options are listed in the appendix. |
| string | This query parameter finds sources from a specific country. The possible options are listed in the appendix. |
Use cases
Use the sources endpoint without any query parameter
The code below shows us how we can use the sources endpoint without any query parameters to get all available sources. Click the “Run” button to get all available sources. The id of the first source will be saved so that we can use it with the everything and top-headlines endpoint.
import requestsimport jsonURL=('https://newsapi.org/v2/top-headlines/sources')header = {'X-Api-Key': "{{API_KEY}}"}response = requests.request("GET", URL, headers=header).json()print(json.dumps(response, indent=4))
We get the category, description, language, URL, country, ID, and name of the sources in response.
- We import the required packages in line 1 and line 2.
- In line 4, we specify the
URLof the endpoint. - In lines 7 to 9, the header is declared.
- We call the
sourcesendpoint in line 10. - The command on line 11 prints the response we got by calling the
sourcesendpoint.
Note: We’ll use the same code throughout the course. The only thing which will change for other endpoints is the
URL.
Use the sources endpoint with a query parameter
The results we got in the previous case can be filtered using query parameters listed in the table above.
In the code below, we’re using the category to get sources for sports. Click the “Run” button to call the endpoint and see the response.
import requestsimport jsonURL=('https://newsapi.org/v2/top-headlines/sources?''category=sports')header = {'X-Api-Key': "{{API_KEY}}"}response = requests.request("GET", URL, headers=header).json()print(json.dumps(response, indent=4))
We can replace the query parameter, category, in line 5 with other query parameters to see how they filter the results.
Use the sources endpoint with multiple query parameters
We can also use multiple query parameters with this endpoint. The code below shows how we can do that. Click the “Run” button to execute the code.
import requestsimport jsonURL=('https://newsapi.org/v2/top-headlines/sources?''category=sports&''language=en')header = {'X-Api-Key': "{{API_KEY}}"}response = requests.request("GET", URL, headers=header).json()print(json.dumps(response, indent=4))
We can also use all three query parameters together to get more filtered results. For this, we’ll just need to add the country parameter in the code above after the other two parameters.