newsapi
![](https://bcentral-packageicons.azureedge.net/images/ballerinax_newsapi_1.1.0.png)
ballerinax/newsapi Ballerina library
Overview
This is a generated connector for News API v2.0.0 OpenAPI specification.
News API used to fetch news(articles, headlines and sources) from news sources and blogs across the web.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Create News API Account
- Obtaining tokens
- Log into News API Account
- Get API key by clicking to
Get API Key
Quickstart
Step 1: Import connector
Import the ballerinax/newsapi module into the Ballerina project.
import ballerinax/newsapi;
Step 2: Create a new connector instance
newsapi:ApiKeysConfig config = { apiKey : "<your apiKey>" }; newsapi:Client myclient = check new newsapi:Client(config, {}, "https://newsapi.org/v2");
Step 3: Invoke connector operation
- You can get top headlines as follows with
listTopHeadlines
method by passing country as a parameter.newsapi:WSNewsTopHeadlineResponse result = check myclient->listTopHeadlines(country="us");
- Use
bal run
command to compile and run the Ballerina program.
Clients![](/images/permalink.svg)
newsapi: Client![](/images/permalink.svg)
This is a generated connector for News API v2.0.0 OpenAPI specification. News API used to fetch news(articles, headlines and sources) from news sources and blogs across the web.
Constructor![](/images/permalink.svg)
Gets invoked to initialize the connector
.
The connector initialization requires setting the API credentials.
Create an News API Account and obtain tokens following this guide.
init (ApiKeysConfig apiKeyConfig, ClientConfiguration clientConfig, string serviceUrl)
- apiKeyConfig ApiKeysConfig - API keys for authorization
- clientConfig ClientConfiguration {} - The configurations to be used when initializing the
connector
- serviceUrl string "https://newsapi.org/v2" - URL of the target service
listSources![](/images/permalink.svg)
function listSources(string? language, string? country, string? category) returns WSNewsSourcesResponse|error
Returns a list of news sources or blogs
Parameters
- language string? (default ()) - Find sources that display news in a specific language. Possible options: ar de en es fr he it nl no pt ru se ud zh. Default: all languages
- country string? (default ()) - Find sources that display news in a specific country. Default: all countries
- category string? (default ()) - Find sources that display news of this category. Default: all categories
Return Type
- WSNewsSourcesResponse|error - Record containing list of news sources
listTopHeadlines![](/images/permalink.svg)
function listTopHeadlines(string? q, string? sources, string? country, string? category, int? page, int pageSize) returns WSNewsTopHeadlineResponse|error
Returns a list of top headlines
Parameters
- q string? (default ()) - Keywords or a phrase to search for
- sources string? (default ()) - Comma-seperated string of source identifiers for news sources or blogs
- country string? (default ()) - Find sources that display news in a specific country. Default: all countries
- category string? (default ()) - Find sources that display news of this category. Default: all categories
- page int? (default ()) - Use this to page through results if total results found is greater than page size
- pageSize int (default 20) - Number of results to return per page (request). 20 is default, 100 is maximum
Return Type
- WSNewsTopHeadlineResponse|error - Record containing list of top headlines
listArticles![](/images/permalink.svg)
function listArticles(int page, int pageSize, string? q, string? qInTitle, string? sources, string? domains, string? excludeDomains, string? 'from, string? to, string? language, string? sortBy) returns WSNewsTopHeadlineResponse|error
Returns list of articles
Parameters
- page int - Use this to page through results
- pageSize int - Number of results to return per page. 20 is default, 100 is maximum
- q string? (default ()) - Keywords or phrases to search for in article title and body. Complete value for query must be URL-encoded
- qInTitle string? (default ()) - Keywords or phrases to search for in article title only. Complete value for qInTitle must be URL-encoded
- sources string? (default ()) - Comma-seperated string of source identifiers (maximum 20) for news sources or blogs
- domains string? (default ()) - Comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict search
- excludeDomains string? (default ()) - Comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from results
- 'from string? (default ()) - Date and optional time for oldest article allowed. This should be in ISO 8601 format (e.g. 2021-02-15 or 2021-02-15T19:07:40) Default: oldest according to pricing plan
- to string? (default ()) - Date and optional time for newest article allowed. This should be in ISO 8601 format (e.g. 2021-02-15 or 2021-02-15T19:07:40) Default: newest according to pricing plan
- language string? (default ()) - Find sources that display news in a specific language. Possible options: ar de en es fr he it nl no pt ru se ud zh .Default: all languages
- sortBy string? (default ()) - Order to sort articles in. Possible options: relevancy, popularity, publishedAt
Return Type
- WSNewsTopHeadlineResponse|error - Record containing list of articles
Records![](/images/permalink.svg)
newsapi: ApiKeysConfig![](/images/permalink.svg)
Provides API key configurations needed when communicating with a remote HTTP endpoint.
Fields
- apiKey string - All requests on the News API needs to include an API key. The API key can be provided as part of the query string or as a request header. The name of the API key needs to be
apiKey
newsapi: WSNewsArticle![](/images/permalink.svg)
Record representing a news article
Fields
- 'source WSNewsSource? - Record representing source of a news articles
- author string? - Author of article
- title string? - Headline or title of article
- description string? - Description or snippet from article
- url string? - Direct URL to article
- urlToImage string? - URL to a relevant image for article
- publishedAt string? - Date and time that article was published, in UTC (+000)
- content string? - Unformatted content of article, where available. This is truncated to 200 chars
newsapi: WSNewsErrorResponse![](/images/permalink.svg)
News API error response
Fields
- status string? - If request was successful or not. Options: ok, error. In case of ok, below two properties will not be present
- code string? - Short code identifying type of error returned
- message string? - Fuller description of error, usually including how to fix it
newsapi: WSNewsSource![](/images/permalink.svg)
Record representing source of a news articles
Fields
- id string? - Identifier of news source. You can use this with our other endpoints
- name string? - Name of news source
- description string? - Description of news source
- url string? - URL of homepage
- category string? - Find sources that display news of this category. Default: all categories
- language string? - Find sources that display news in a specific language. Possible options: ar de en es fr he it nl no pt ru se ud zh .Default: all languages
- country string? - Find sources that display news in a specific country. Default: all countries
newsapi: WSNewsSourcesResponse![](/images/permalink.svg)
Record representing list sources
Fields
- status string? - If request was successful or not. Options: ok, error. In case of ok, below two properties will not be present
- sources WSNewsSource[]? - Results of request
newsapi: WSNewsTopHeadlineResponse![](/images/permalink.svg)
Record representing top headlines
Fields
- status string? - If request was successful or not. Options: ok, error. In case of error a code and message property will be populated
- totalResults int? - Total number of results available for request. Only a limited number are shown at a time though, so use page parameter in requests to page through them
- articles WSNewsArticle[]? - Results of request
Import
import ballerinax/newsapi;
Metadata
Released date: over 2 years ago
Version: 1.1.0
License: Apache-2.0
Compatibility
Platform: any
Ballerina version: slbeta6
GraalVM compatible: Yes
Pull count
Total: 1983
Current verison: 123
Weekly downloads
Keywords
Lifestyle & Entertainment/News & Lifestyle
Cost/Freemium
Contributors
Dependencies