Registry / messaging / nostr-fetch

nostr-fetch

JSON →
library0.17.0jsnpmunverified

A utility library for JavaScript/TypeScript applications to fetch past events from Nostr relays with built-in concurrency control, timeout handling, and connection management. Version 0.17.0 is current, with a stable API and frequent releases. Unlike raw WebSocket handling or libraries like nostr-tools, nostr-fetch provides a high-level fetch interface that abstracts relay subscription lifecycle, retries, and pagination. It supports both Node.js and browser environments, ships TypeScript types, and is ESM-only. Key differentiators include automatic relay connection pooling, custom fetch strategies, and event validation against filters.

npm install nostr-fetch
INSTALL
IMPORT
SIG · NOSTR-FETCH
N
nostr-fetch
messagingjavascriptv0.17.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetchEvents
import { fetchEvents } from 'nostr-fetch'
import fetchEvents from 'nostr-fetch'
Named export; default export is a function that returns a promise for relay list
NostrFetcher
import { NostrFetcher } from 'nostr-fetch'
import { nostrFetcher } from 'nostr-fetch'
Class export; use for custom fetcher instances
FetchResult
import type { FetchResult } from 'nostr-fetch'
import { FetchResult } from 'nostr-fetch'
TypeScript type, use 'import type' to avoid bundling issues

Shows basic usage of fetchEvents to retrieve 10 text notes from two public relays with default options.

import { fetchEvents } from 'nostr-fetch'; const relays = ['wss://relay.damus.io', 'wss://relay.nostr.band']; const filter = { kinds: [1], limit: 10, }; async function main(): Promise<void> { const events = await fetchEvents(relays, filter, { skipVerification: false, abortSignal: undefined, }); console.log('Events:', events); } main().catch(console.error);
Debug
Known issues
deprecatedfetchEvents with callback signature is deprecated in v0.15.0
fix
Use async/await instead of callback; callback support removed in v0.17.0
affects: >=0.15.0
breakingIn v0.16.0, default export signature changed from function to object with methods
fix
Update imports to use named exports; default export is now { getDefaultRelays, setDefaultRelays }
affects: >=0.16.0 <0.17.0
breakingRelay URL validation now rejects URLs without explicit path (e.g., 'wss://relay.com') as of v0.14.0
fix
Always include trailing slash in relay URLs: 'wss://relay.com/'
affects: >=0.14.0
deprecatedThe option 'timeout' has been renamed to 'requestTimeout' in v0.12.0
fix
Replace 'timeout' with 'requestTimeout' in fetch options object
affects: >=0.12.0
gotchafetchEvents will connect to all provided relays simultaneously; to avoid overwhelming connections, use a throttled fetcher
fix
Create a NostrFetcher instance with custom maxConns or batch relays into groups
affects: >=0.0.0
gotchaThe library does not handle event verification by default; set skipVerification: false to validate signatures
fix
Add skipVerification: false in fetch options; requires nostr-tools for verification
affects: >=0.0.0
breakingIn v0.10.0, the package switched to ESM-only, dropping CommonJS support
fix
Use dynamic import() for CommonJS projects, or migrate to ESM
affects: >=0.10.0
Errors
Common errors & fixes
SyntaxError: Unexpected token 'export'
Node.js version <12 or using require() in CommonJS with ESM-only package
fix
Use import() or upgrade Node.js >=12; or set type: 'module' in package.json
TypeError: fetchEvents is not a function
Importing default export as a function, but it's now an object (v0.16+)
fix
Use named import: import { fetchEvents } from 'nostr-fetch'
Error: no relay urls provided
Passed empty array or undefined relays to fetchEvents
fix
Provide at least one relay URL string in the array
Error: Invalid relay URL: wss://relay.damus.io
Missing trailing slash or invalid format (pre-v0.14 allowed, v0.14+ rejects)
fix
Add trailing slash: 'wss://relay.damus.io/'
Upgrade
Version history
0.17.0latest on npm
Audit
Dependencies
nostr-toolsrequiredUses nostr-tools types (Filter, Event) and relay abstraction
cross-fetchoptionalPolyfill fetch for Node.js <18; browser users can skip if native fetch works
Agent activity
20 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
nostr-fetch — npm install nostr-fetch · libregistry