Repository navigation
Expand file tree
/
Copy pathbasic.ts
More file actions
66 lines (53 loc) · 1.93 KB
/
Copy pathbasic.ts
File metadata and controls
66 lines (53 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Basic 402-announce example
*
* Announces a paid API on Nostr so AI agents can discover it.
*
* Prerequisites:
* - A 64-character hex Nostr secret key (generate with nostr-tools or any Nostr key tool)
* - One or more Nostr relay URLs
*
* Run:
* npx tsx examples/basic.ts
*/
import { announceService } from '402-announce'
const NOSTR_SECRET_KEY = process.env.NOSTR_SECRET_KEY
if (!NOSTR_SECRET_KEY) {
console.error('Set NOSTR_SECRET_KEY environment variable (64-char hex)')
process.exit(1)
}
const handle = await announceService({
secretKey: NOSTR_SECRET_KEY,
relays: ['wss://relay.damus.io', 'wss://relay.primal.net'],
// Unique identifier for this listing — same pubkey + identifier updates the listing
identifier: 'weather-api',
name: 'Weather API',
urls: ['https://weather.example.com/api'],
about: 'Real-time weather data behind an L402 paywall',
// Per-capability pricing
pricing: [
{ capability: 'current_weather', price: 5, currency: 'sats' },
{ capability: 'forecast_7day', price: 20, currency: 'sats' },
],
// Accepted payment methods
paymentMethods: [['l402', 'lightning'], ['cashu']],
// Optional: topic tags for search/filtering
topics: ['weather', 'data', 'ai'],
// Optional: describe capabilities in detail (stored in event content)
capabilities: [
{ name: 'current_weather', description: 'Current conditions for a given location' },
{ name: 'forecast_7day', description: '7-day forecast for a given location' },
],
version: '1.0.0',
})
console.log('Service announced on Nostr!')
console.log(' Event ID:', handle.eventId)
console.log(' Pubkey: ', handle.pubkey)
// Keep the process alive so relay connections stay open.
// In a real server, you would call handle.close() on shutdown.
process.on('SIGINT', () => {
console.log('\nClosing relay connections...')
handle.close()
process.exit(0)
})
console.log('\nPress Ctrl+C to disconnect from relays and exit.')