Quick Start Guide

Universal 5-step guide to handle Nostr events in any programming language. Connect to Shugur's HA relay cluster and get publishing and receiving events in minutes.

Choose Your Language

Same 5 steps, different syntax - pick your preferred programming language

JavaScript Quick Start

5 steps to handle Nostr events using native tools

1

1. Install Requirements

Use native WebSocket and Web Crypto API - no external dependencies needed

JavaScript
// No installation required! 
// Uses native browser APIs:
// - WebSocket for relay connection
// - crypto.subtle for cryptographic operations
// - TextEncoder/TextDecoder for data conversion

console.log('Ready to use native web APIs!')
2

2. Generate Keypair

Create Nostr keypair using native crypto.subtle API

JavaScript
// Generate random 32-byte private key
const privateKey = crypto.getRandomValues(new Uint8Array(32))

// Convert to hex string
const sk = Array.from(privateKey, b => b.toString(16).padStart(2, '0')).join('')

// Derive public key using secp256k1 (simplified example)
// In production, use a proper secp256k1 implementation
const pk = await derivePublicKey(sk)

console.log('Private key:', sk)
console.log('Public key:', pk)
3

3. Connect to Relay

Establish WebSocket connection to wss://shu01.shugur.net

JavaScript
// Native WebSocket connection
const ws = new WebSocket('wss://shu01.shugur.net')

ws.onopen = () => {
  console.log('Connected to Shugur relay!')
}

ws.onmessage = (event) => {
  const message = JSON.parse(event.data)
  console.log('Received:', message)
}

ws.onerror = (error) => {
  console.error('WebSocket error:', error)
}
4

4. Subscribe & Listen

Send REQ message to subscribe to events and listen for responses

JavaScript
// Subscribe to recent text notes
const subscription = [
  "REQ",
  "sub_" + Math.random().toString(16).slice(2), // Subscription ID
  {
    kinds: [1],    // Text notes
    limit: 10      // Last 10 events
  }
]

// Send subscription
ws.send(JSON.stringify(subscription))

// Listen for events
ws.onmessage = (event) => {
  const data = JSON.parse(event.data)
  if (data[0] === 'EVENT') {
    console.log('Received note:', data[2].content)
  }
}
5

5. Publish Event

Create, sign, and publish your first Nostr event

JavaScript
// Create event
const event = {
  kind: 1,
  created_at: Math.floor(Date.now() / 1000),
  tags: [],
  content: 'Hello Nostr from native JavaScript!',
  pubkey: pk
}

// Calculate event ID (SHA256 of serialized event)
const eventData = JSON.stringify([0, event.pubkey, event.created_at, event.kind, event.tags, event.content])
const encoder = new TextEncoder()
const hash = await crypto.subtle.digest('SHA-256', encoder.encode(eventData))
event.id = Array.from(new Uint8Array(hash), b => b.toString(16).padStart(2, '0')).join('')

// Sign event (implement secp256k1 signing)
event.sig = await signEvent(event, sk)

// Publish event
ws.send(JSON.stringify(['EVENT', event]))
console.log('Published event!')