Module nytimes.articlesearch
ballerinax/nytimes.articlesearch Ballerina library
Overview
This is a generated connector for New York Times Article Search API v1.0.0 OpenAPI specification. With the Article Search API, you can search New York Times articles from Sept. 18, 1851 to today, retrieving headlines, abstracts, lead paragraphs, links to associated multimedia and other article metadata. Use the Article Search API to look up articles by keyword. You can refine your search using filters and facets.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Create NYTimes developer account
- Obtain tokens
- Log into NYTimes Developer Portal by visiting https://developer.nytimes.com/accounts/login
- Register an app and obtain the API Key by following the process summarized here.
Quickstart
To use the New York Times Article Search connector in your Ballerina application, update the .bal file as follows:
Step 1: Import connector
First, import the ballerinax/nytimes.articlesearch
module into the Ballerina project.
import ballerinax/nytimes.articlesearch as na;
Step 2: Create a new connector instance
Create a articlesearch:ApiKeysConfig
with the API key obtained, and initialize the connector with it.
na:ApiKeysConfig config = { apiKey: "<API_KEY>" } na:Client baseClient = check new Client();
Step 3: Invoke connector operation
-
Now you can use the operations available within the connector. Note that they are in the form of remote operations.
Following is an example on how to look up articles by keyword using the connector. You can now search for NYT articles by keywords, filters and facets. Here we have searched for articles with the word "election".
Look up articles by keyword
public function main() { na:InlineResponse200|error response = baseClient->articleSearch("election"); if (response is na:InlineResponse200) { na:InlineResponse200Response? inlineResponse200Response = response?.response; na:Doc[]? docs = inlineResponse200Response?.docs; if !(docs is ()) { foreach var doc in docs { log:printInfo(doc?.'abstract.toString() + "\n"); } } } else { log:printError(response.toString()); } }
-
Use
bal run
command to compile and run the Ballerina program.