Official React hooks SDK for the Newsdata.io News
API. Drop-in useLatestNews, useArchiveNews, useCryptoNews,
useMarketNews, useNewsCount, … hooks plus a <NewsDataProvider> to share
one client. Built on the same proven core as the Node client — validation,
typed errors, retries with exponential backoff — and ships first-class
TypeScript definitions.
Zero runtime dependencies, no build step, React 18+ as a peer dependency.
npm install newsdataapi
# react is a peer dependency
npm install reactimport{NewsDataProvider,useLatestNews}from'newsdataapi';functionHeadlines(){const{ data, error, isLoading }=useLatestNews({q: 'bitcoin',country: ['us','gb'],language: 'en',});if(isLoading)return<p>Loading…</p>;if(error)return<p>Error: {error.message}</p>;return(<ul>{data.results.map((article)=>(<likey={article.article_id}><ahref={article.link}>{article.title}</a></li>))}</ul>);}exportdefaultfunctionApp(){return(<NewsDataProviderapiKey={process.env.REACT_APP_NEWSDATA_API_KEY}><Headlines/></NewsDataProvider>);}| Hook | Endpoint | Notes |
|---|---|---|
useLatestNews(params) | /1/latest | Real-time news |
useArchiveNews(params) | /1/archive | Historical news |
useNewsSources(params) | /1/sources | Available sources (single page) |
useCryptoNews(params) | /1/crypto | Cryptocurrency news |
useMarketNews(params) | /1/market | Market / financial news |
useNewsCount(params) | /1/count | Aggregate counts (requires from_date, to_date) |
useCryptoCount(params) | /1/crypto/count | Aggregate crypto counts |
useMarketCount(params) | /1/market/count | Aggregate market counts |
Every hook has the same shape:
const{ data, error, isLoading, refetch }=useLatestNews(params,options);data— the API response (nulluntil the first fetch resolves)error— a typedNewsdataError(nullon success)isLoading—truewhile a request is in flightrefetch()— re-run the request; returns the underlyingPromise
useLatestNews(params,{enabled: false})// skip the request until enabledoptions.enabled (default true) defers fetching — handy when params aren't
ready (e.g. waiting on user input).
Values can be a single string or an array of strings (sent comma-joined),
and parameter names are case-insensitive — qInTitle and qintitle are
equivalent:
useLatestNews({country: ['us','gb'],language: 'en',size: 20});Inline param objects are safe — the hook compares by value, not reference, so re-renders only re-fetch when the values change.
Two ways to wire it up:
// 1. Let the provider construct the client.<NewsDataProviderapiKey="..."options={{timeout: 10_000,maxRetries: 3}}><App/></NewsDataProvider>// 2. Or pass your own pre-built client (full control over its lifecycle).import{NewsDataApiClient}from'newsdataapi';constclient=newNewsDataApiClient(apiKey);<NewsDataProviderclient={client}><App/></NewsDataProvider>Anywhere inside the provider you can grab the client directly:
import{useNewsDataClient}from'newsdataapi';functionExportButton(){constclient=useNewsDataClient();return<buttononClick={()=>client.archiveApi({q: 'x'}).then(save)}>Export</button>;}All hook errors are instances of the typed hierarchy from the core SDK:
import{NewsdataValidationError,NewsdataAuthError,NewsdataRateLimitError,NewsdataApiError,NewsdataNetworkError,}from'newsdataapi';if(errorinstanceofNewsdataRateLimitError){console.log('retry after',error.retryAfter,'seconds');}NewsdataError (catch-all base)
├── NewsdataValidationError (.param)
├── NewsdataApiError (.statusCode, .responseBody)
│ ├── NewsdataAuthError (401 / 403)
│ ├── NewsdataRateLimitError (429; .retryAfter)
│ └── NewsdataServerError (5xx)
└── NewsdataNetworkError (.cause)
Validation errors are thrown before the request is sent (no API quota
spent) — e.g. setting q and qInTitle together, an unsupported parameter
for that endpoint, or missing from_date/to_date on a count endpoint.
You can also use the underlying client without React — same surface as
newsdata-nodejs-client:
import{NewsDataApiClient}from'newsdataapi';constclient=newNewsDataApiClient(apiKey,{timeout: 10_000});// scroll: follow nextPage cursors and merge.constmerged=awaitclient.latestApi({q: 'news',scroll: true,maxResult: 200});// paginate: async generator, one page at a time.forawait(constpageofclient.latestApi({q: 'news',paginate: true,maxPages: 5})){console.log(page.results.length);}npm install
npm test# node:test, 34 tests, no API key requiredOfficial Newsdata.io clients across languages and runtimes:
- Python — newsdataapi/python-client (PyPI)
- Node.js — newsdataapi/newsdata-nodejs-client (npm)
- PHP — newsdataapi/php-client (Packagist)
- Java — newsdataapi/newsdata-java-sdk (Maven Central)
- .NET — newsdataapi/newsdata-dotnet-sdk (NuGet)
- Go — newsdataapi/newsdata-go-client (pkg.go.dev)
- Dart / Flutter — newsdataapi/newsdata-flutter-client (pub.dev)
- MCP Server (AI assistants) — newsdataapi/newsdata.io-mcp (PyPI)
Also see free news datasets for ML / NLP work.
