The Request and Response Structure
Let's look at the request and response structure of News API.
We'll cover the following...
Request structure
A request for News API consists of only two elements:
- The authorization element.
- The required endpoint.
The authorization element
The API key is used for authorization for News API users. We can include the API key in the GET request through either of the following strategies:
- By including it as a query parameter called
apiKey. - By including the API key in the HTTP header. The element’s name will be
X-API-KeyorAuthorization.
Note: In this course, we’ll send the API key in the header as
X-API-key.
The required endpoint
We can use one of these three endpoints:
sourceseverythingtop-headlines
We’ll discuss these endpoints and their uses with different query parameters in the upcoming lessons.
The response structure
News API’s response differs based on whether the request is successful or not. Let’s cover them one by one.
For a successful request
The response for a successful sources endpoint request is different from the response of everything and top-headlines. These response structures are as follows:
For the sources endpoint
The response for a successful request for this endpoint contains two main elements:
status: This isokin this case.sources: This contains all the available sources.
For the everything and top-headlines endpoints
The response for a successful request contains three main elements:
status: This isokin this case.totalResults: This tells us the total number of articles we got in the response.articles: This contains all the news articles.
For an unsuccessful request
The response for a successful request is the same for all three endpoints. It contains three main elements:
status: This iserrorin this case.code: This is a short code identifying the type of error returned.message: This contains a description of the error and its fix. Here are the possible options for these error messages:
Message | Description |
| The API key has been disabled. |
| The API key has no more requests available. |
| The API key hasn't been entered correctly. Double check it and try again. |
| The API key is missing from the request. Append it to the request either as a query parameter or in the header. |
| We've included a parameter in your request which is currently not supported. Check the message property for more details. |
| Required parameters are missing from the request and it cannot be completed. Check the message property for more details. |
| We have been rate limited. Allow some time to pass before attempting the request again. |
| We’ve requested too many sources in a single request. Try splitting the request into two smaller requests. |
| We’ve requested a source that doesn’t exist. |
| This message is populated for an unexpected error. We can try the request again shortly. |