The official AllNewsAPI SDK for the JavaScript programming language.
Easily access real-time and historical news articles and headlines from multiple sources around the world.
npm install allnewsapi
or
yarn add allnewsapi
AllNewsAPI uses API keys for authentication. To get started, sign up for free to get an API key
Next, import and initialize the SDK with your API key.
import NewsAPI from 'allnewsapi';
// Initialize the SDK
const newsApi = new NewsAPI('your-api-key');
// Basic search
newsApi.search({ q: 'bitcoin' })
.then(results => console.log(results))
.catch(err => console.error(err));
// Get headlines
newsApi.headlines({ category: 'technology', max: 10 })
.then(results => console.log(results))
.catch(err => console.error(err));
// Advanced search
newsApi.search({
q: 'elon musk',
lang: ['en', 'fr'],
category: 'technology',
max: 10,
sortby: 'relevance'
})
.then(results => console.log(results))
.catch(err => console.error(err));
Creates a new instance of the NewsAPI client.
apiKey
(string, required): Your AllNewsAPI API key.
Search for news articles based on various filters and options. 🔗 See Full API Documentation
options
(object):q
(string): Keywords to search for.startDate
(string | Date): Start date for search (format:YYYY-MM-DD
orYYYY-MM-DD HH:MM:SS
or a JavaScriptDate
object).endDate
(string | Date): End date for search (format:YYYY-MM-DD
orYYYY-MM-DD HH:MM:SS
or a JavaScriptDate
object).content
(boolean): Whether to include full article content.lang
(string | string[]): Language(s) to filter by (e.g.,'en'
,['en', 'fr']
).country
(string | string[]): Country/countries to filter by (ISO 3166-1 alpha-2 codes).region
(string | string[]): Region(s) to filter by.category
(string | string[]): Category/categories to filter by (e.g.,'technology'
,'sports'
).max
(number): Maximum number of results to return.attributes
(string | string[]): Specific attributes to search in (title
,description
,content
).page
(number): Page number for pagination.sortby
(string): Sort results by'publishedAt'
or'relevance'
.publisher
(string | string[]): Filter by publisher(s).format
(string): Response format ('json'
,'csv'
, or'xlsx'
).
Promise<Object>
: Resolves with the search results.
Get the latest headlines based on various filters and options. 🔗 See Full API Documentation
options
(object):q
(string): Keywords to search for.startDate
(string | Date): Start date for search (format:YYYY-MM-DD
orYYYY-MM-DD HH:MM:SS
or a JavaScriptDate
object).endDate
(string | Date): End date for search (format:YYYY-MM-DD
orYYYY-MM-DD HH:MM:SS
or a JavaScriptDate
object).content
(boolean): Whether to include full article content.lang
(string | string[]): Language(s) to filter by (e.g.,'en'
,['en', 'fr']
).country
(string | string[]): Country/countries to filter by (ISO 3166-1 alpha-2 codes).region
(string | string[]): Region(s) to filter by.category
(string | string[]): Category/categories to filter by (e.g.,'technology'
,'sports'
).max
(number): Maximum number of results to return.attributes
(string | string[]): Specific attributes to search in (title
,description
,content
).page
(number): Page number for pagination.sortby
(string): Sort results by'publishedAt'
or'relevance'
.publisher
(string | string[]): Filter by publisher(s).format
(string): Response format ('json'
,'csv'
, or'xlsx'
).
Promise<Object>
: Resolves with the headlines results.
startDate
andendDate
accept either a string (YYYY-MM-DD
) or a DateTime string (YYYY-MM-DD HH:MM:SS
)- When
content: true
, full article content will be included. - If
format
is set to'csv'
or'xlsx'
, results will be returned in the corresponding format (binary response handling required).
To build the project locally:
npm install
npm run build
The output files will be generated in the dist/
folder.