finlight client for Rust
Breaking release. 0.2 speaks finlight API v3 only (REST
/v3, WebSocketpayloadVersion3, v3 webhooks) and does not run 0.1 code. Read Migrating from 0.1 and the changelog first. To stay on API v2, keepfinlight-client = "0.1".
Async client for finlight.me: search financial news, look up articles, companies and sources, follow live articles over WebSocket, and verify webhooks. The crate is finlight-client; the library is imported as finlight.
Versions
| SDK version | API version | Status |
|---|---|---|
| 0.2.x | v3 (/v3, WebSocket payloadVersion 3, webhooks v3) |
current |
| 0.1.x | v2 | no further releases |
SDK and API versions are independent: new API fields and enum values arrive as 0.2.x patch releases. These SDKs speak API v3: TypeScript 4.x, Python 3.x, PHP 2.x, Go /v2, .NET 2.x, Java 1.x, Ruby 1.x, Rust 0.2.
Cargo never moves "0.1" to 0.2. A wildcard like "*" does, so pin the minor version.
Requirements
- Rust 1.85 or newer (edition 2024)
- tokio 1.47 or newer (the SDK spawns one task per stream and needs a tokio runtime)
- reqwest 0.13 (rustls), serde 1, serde_json 1.0.142 or newer; the WebSocket protocol is built in
- TLS through rustls with the aws-lc-rs provider; REST and streams both verify against the operating system's trust store (rustls-platform-verifier), so a corporate CA installed there works for both; no OpenSSL
Installation
Quick start
use ;
async
The SDK sends only the parameters you set. It adds no defaults: the server decides limit (20), languages (["en"]) and the time range. Parameter names are struct fields, so a misspelled name does not compile; SearchParams::from_json rejects unknown keys with Error::Validation before any request.
Search articles
articles().search() returns one page. articles().search_all() walks the cursor for you.
use ;
async
search_allfreezes the request body at the call. Later pages only changecursor, so the cursor stays valid.- It stops at
has_more: false, atnext_cursor: Noneor aftermax_pages(default 10). At the limit it logs a warning,truncated()is true andnext_cursor()lets you continue. - Every page is a metered call. Iterate a result once: either articles (
next(), or as afuturesStream) or pages (next_page()); the other way then returnsError::Validation. fromandtotake aSystemTime(sent as UTC with milliseconds),DateInput::date(y, m, d)(sent asYYYY-MM-DD) or a string (sent as is). They apply to theorder_bytimestamp.source_typesandexclude_source_typesfilter by source type:SourceType::NEWSWIRE,PRESS_RELEASE,FINANCIAL_MEDIA,GENERAL_MEDIA,AGGREGATOR,BLOG,GOVERNMENT. Articles of unclassified sources never matchsource_typesand are kept byexclude_source_types. Both filter inside the source pool and never add a source.StreamParamsandRawStreamParamstake them too.- Entries that cannot be decoded are skipped, logged and listed in
page.skipped. The rest of the page stays usable.
Look up articles, companies and sources
use ;
async
- A 404 is
Error::Apiwithapi_kind() == ApiErrorKind::NotFound, not an empty result. - Articles indexed before the id rollout have no id lookup. Use
get_by_linkfor them. - Company endpoints need the
entityAccessentitlement, otherwise they answer 403 (ApiErrorKind::Forbidden). - The SDK keeps no cache of company records:
companies().get()always sends a plain request, and the article payload already carries the main company fields.cache_info()gives you theETagandCache-Controlof a record. To save credits, keep records yourself and do not call again withinmax-age. To check a kept record, pass its ETag toget_if_none_match; it returnsOk(None)on 304. A 304 costs 1 credit too. - Lookups cost credits like article calls: every request of
companies()(search,get_many,get,get_if_none_match) andsources().list()costs 1 credit,get_manywith several ids included. A 304 revalidation and errors cost 1 credit too. CompanyListandSourceListdereference to slices and haveskipped()for entries that could not be decoded.
Absent, null and empty
Some keys depend on your plan and on include. A missing key, null and [] mean different things, and the SDK keeps them apart.
use ;
- Missing key: not requested, not in your plan, or the raw channel.
null: requested and entitled, but there is no value (for example no text forcontent).[]: a real empty list (for example tagged, but no company found).contentandsentimentareField<T>(Absent,Null,Value).companies,events,events[].company_idsandSource::has_contentareOption<_>:Nonemeans absent, never "empty".content_present(),sentiment_present(),companies_present()andevents_present()tell whether the key was in the JSON (nullincluded).- Unknown keys from newer servers are ignored;
raw_json()returns the JSON as received. Writing a model back with serde keeps absent keys absent. - Vocabularies are open:
CompanyRole::Other("...")keeps a value this SDK does not know. Compare with the constants, e.g.role == CompanyRole::ACQUIRER.
Real-time streams
client.stream() follows enriched articles, client.raw_stream() the raw skeleton before enrichment. A background task owns the socket: it pings every 25 s, reconnects when no pong arrives for 60 s, renews the connection after 115 minutes, re-subscribes after every reconnect and drops duplicates.
use ;
use StreamExt;
async
Things to know:
- Connection limit. Your plan's WebSocket limit is shared by enriched streams, raw streams and older SDK versions. Do not run old and new streams side by side. When the limit is reached the SDK waits at least 60 s and tries again;
StreamOptions::takeover(true)replaces your stalest connection instead. - Two hour limit. The gateway ends every connection after 2 h. The SDK opens a new connection before that, re-subscribes, and closes the old one once the new one is admitted. When your plan is at its connection limit (for example a limit of 1), the old connection still counts and the new dial is refused; the SDK then closes the old connection and dials again at once, so the gap is about one dial long. Only if that dial is refused too does the usual wait of at least 60 s apply.
- Initial push. After each subscribe the enriched stream sends the latest matching article. It can be old. The SDK drops it when it was already delivered on this stream.
- Large frames. Live frames over 32,000 bytes are dropped by the server without notice. This can happen with
include: contenton long articles. Fetch the text witharticles().get(id, ...)instead. on_admittedfires before the server checks your subscribe. It does not mean the subscribe was accepted.- The SDK does not backfill.
on_reconnectgives youlast_seen_atso you can fill gaps through REST. - Dropping the stream or
stream.close().awaitcloses the socket with 1000 and stops all background work. - A stream ends with an
Errfor a refused key, a plan without WebSockets, an invalid subscribe, close code 1008 and aconnection_limitpreemption (Error::Preempted). Other problems go toon_errorand the stream keeps running. Panics in hooks are caught and logged. - Used-up WebSocket quota. When the quota of the period is used up (refused dial or
quota_exceededpreemption), the stream does not end: it tries again every hour until the quota allows it. Each wait is logged as a warning and reported toon_quota_wait. SetConfig::quota_reconnecttofalseto end the stream with the error instead;quota_reconnect_intervalchanges the interval. REST calls never wait: a monthly 429 fails at once. raw_streamhas noincludeand no enrichment filters. It does takesource_typesandexclude_source_types.
Webhooks
webhooks::construct_event(raw_body, headers, secret, &options) checks the signature and the timestamp (300 s tolerance by default), then reads the body. It returns WebhookPayload::Event, or WebhookPayload::WrongPayloadVersion for a correctly signed body in the old format.
use Bytes;
use ;
use Error;
use ;
// Take the body as Bytes: never verify re-serialized JSON.
async
The signature covers the raw bytes. Get them before any JSON parser runs:
-
axum: take
axum::body::Bytes(notJson<T>) andHeaderMap. -
actix-web: take
web::Bytesand passreq.headers()values toconstruct_event_from_values. -
warp:
warp::body::bytes()withwarp::header::headers_cloned(). -
hyper: collect the body into
Bytesand passparts.headers. -
headerstakes anhttp::HeaderMap, aHashMaporBTreeMapof strings, or(name, value)pairs. Names match case-insensitively, CGI names (HTTP_X_WEBHOOK_TIMESTAMP) included. -
Deliveries can repeat. Dedupe on
event_id. -
Answer with 2xx within 5 s and do slow work afterwards.
-
Never answer with 410: it disables the webhook at once.
-
is_test()is true for test deliveries from the dashboard. -
webhooks::verify_signature(raw_body, signature, timestamp, secret, &options)checks only the signature;construct_event_from_valuestakes the two header values directly. -
The SDK does not manage webhooks. Create and change them in the finlight dashboard.
Errors and retries
use ;
async
Every error is one finlight::Error variant; kind() names it as text. API errors carry status(), code(), title(), detail(), request_id(), errors() (FieldIssue), api_kind() and raw() (the body as received). Each logical call sends its own X-Request-Id; retries reuse it. Causes stay reachable through std::error::Error::source.
finlight::Error kind()
├── Error::Api(ApiError) api (api_kind: validation_failed, cursor_invalid, cursor_mismatch,
│ unauthorized, forbidden, not_found, rate_limited
│ (limit_type, retry_after), internal_error, other)
├── Error::Validation(ValidationError) validation (local, before any request)
├── Error::Network(NetworkError) network
├── Error::Timeout(TimeoutError) timeout
├── Error::Decode(DecodeError) decode
├── Error::WebhookVerification(..) webhook_verification (reason)
└── Error::Preempted(PreemptedError) preempted (reason, new_lease_id)
Retries:
| Answer | Retry |
|---|---|
| 400, 401, 403, 404 | never |
| 429 monthly / unknown | never |
| 429 burst | once, after exactly Retry-After (10 s when the header is missing); on by default, RetryOnBurst::Off turns it off |
| 5xx, network errors, timeouts | up to 3 times with growing, jittered waits |
Cancel a call by dropping its future, for example with tokio::time::timeout or tokio::select!. Waits end at once and nothing is retried after that. Config::total_timeout bounds a whole call, retries and the burst wait included.
Configuration
Config::new(key) gives the defaults; Config::builder(key)....build()? checks them. Durations are std::time::Duration.
| Field | Default | Meaning |
|---|---|---|
api_key |
- | Required, not empty. |
base_url |
https://api.finlight.me |
REST base URL. |
wss_url |
wss://wss.finlight.me |
WebSocket URL; raw uses <wss_url>/raw. |
timeout |
30 s | Timeout of one HTTP attempt. |
total_timeout |
none | Optional limit for a whole call, retries and waits included. |
max_retries |
3 | Retries for 5xx, network errors and timeouts. |
retry_base_delay / retry_max_delay |
0.5 s / 8 s | Backoff between retries (with jitter). |
retry_on_burst |
RetryOnBurst::Once |
Once or Off for 429 burst answers. |
burst_wait |
10 s | Wait before the burst retry when the 429 has no Retry-After. |
max_pages |
10 | Page limit of search_all. |
max_response_bytes |
32 MiB | Largest REST response body. |
ping_interval / watchdog_timeout / rotation_interval |
25 s / 60 s / 115 min | Stream ping, pong watchdog and rotation. |
reconnect_base_delay / reconnect_max_delay |
1 s / 30 s | Stream reconnect backoff. |
long_backoff |
60 s | Wait after "too many connections" and 429 dial refusals. |
quota_reconnect / quota_reconnect_interval |
true / 1 h |
Streams only: when the WebSocket quota is used up, retry every hour (false ends the stream). REST never waits for a new quota. |
search_failed_threshold |
5 | After this many SEARCH_FAILED in a row an error goes to on_error. |
dedupe_cache_size |
1000 | Entries of the duplicate filter per stream. |
max_message_bytes |
32 MiB (min. 16 MiB) | Largest incoming stream message. |
connect_timeout |
30 s | WebSocket dial and handshake timeout. |
http_client |
- | Your own reqwest::Client for REST, e.g. for a proxy. Its own timeouts then apply and timeout is not used; switch off its own retries. The SDK never closes it. |
ws_connector |
- | Your own WsConnector that opens the byte stream for stream dials (e.g. a proxy tunnel); the SDK adds TLS (operating system trust store) and the handshake. |
Tests use tokio's paused clock (tokio feature test-util); there is no separate clock option.
Logging
The SDK logs through tracing with the target finlight. Without a subscriber nothing is printed; it never writes to stdout or stderr itself and installs no global handlers. Retries, reconnects and quota waits are warnings; per-message details are debug. The API key, the webhook secret, signatures and full bodies are never logged, and Debug output of Config, Client and errors leaves them out. The SDK speaks the WebSocket protocol itself, so no transport library logs stream frames, at any level.
Data notes
- Some old articles have
published_at1970-01-01T00:00:00.000Z. The SDK delivers it as it is;summaryandcontentarrive asNone/Field::Nullwhen there is no text. - On articles tagged before roles existed,
companies[].roleandrelevanceareNone, andeventsisSome([]). - Articles indexed before the id rollout answer 404 on
articles().get();get_by_linkfinds them. available_atis alwaysNoneon raw, andNonefor articles from before its rollout.source.typeisNonewhen the source is not classified or the article was indexed before source types existed. It can lagtypeinsources().list()after a reclassification.
Migrating from 0.1
The full guide is on docs.finlight.me. The main changes:
| 0.1 (API v2) | 0.2 (API v3) |
|---|---|
use finlight_client::... |
use finlight::... (the crate is still finlight-client) |
client.articles.fetch_articles(&GetArticlesParams {..}) |
client.articles().search(&SearchParams::new()...), search_all(...) |
client.articles.fetch_article_by_link(&GetArticleByLinkParams {..}) |
client.articles().get_by_link(link, &options), get(id, &options) |
client.sources.get_sources() |
client.sources().list() |
client.websocket.stream(GetArticlesWebSocketParams {..}) |
client.stream(StreamParams::new()..., StreamOptions::new()).await? |
client.raw_websocket.stream(..) |
client.raw_stream(RawStreamParams::new()..., options).await? |
WebSocketClient::new(config, WebSocketOptions {..}) |
stream settings in Config (ping_interval, watchdog_timeout, ...) and hooks in StreamOptions |
construct_webhook_event(body, signature, secret, timestamp) |
webhooks::construct_event(body, &headers, secret, &options) or construct_event_from_values(...) |
Error::Api { status, body }, Error::Blocked |
Error::Api(ApiError) with status(), code(), api_kind(); Error::Preempted |
publish_date: DateTime<Utc> |
published_at: Option<String> + published_at_time() |
created_at |
first_seen_at |
revised_date |
revised_at |
images: Vec<String> |
media: Vec<Media> |
source: String |
source.domain |
sentiment, confidence |
sentiment: Field<Sentiment> (label, confidence) |
include_content, include_entities |
include([IncludeBlock::CONTENT, IncludeBlock::COMPANIES, IncludeBlock::EVENTS]) |
page, page_size |
limit, cursor (search_all pages for you) |
language |
languages(["en"]) |
is_update |
StreamEventKind::Updated |
| webhook body = article | event.data + event.event_id |
Switching a webhook to v3, in four steps:
- Deploy the new SDK.
- Answer
WrongPayloadVersionwith 2xx and keep itsrawJSON if you need the data. - Switch the webhook to
payloadVersion3 in the finlight dashboard. - Wait about 2 h. Retries that were already queued still arrive in the old format.
Development
FINLIGHT_API_KEY=... FINLIGHT_BASE_URL=https://api.dev.finlight.me \
FINLIGHT_WSS_URL=wss://wss.dev.finlight.me
The contract kit lives in conformance/ (CONTRACT_VERSION, sha256 MANIFEST) and is not published. Every kit case runs as its own test in tests/conformance.rs; CONFORMANCE.md maps cases to tests, and the suite fails when the copy does not match the MANIFEST or a case has no test. Live tests run only against dev and need all three variables; production hosts are refused.
License
MIT