Hello, rust-nostr!
Now that you’ve installed the SDK, it’s time to write your first nostr program.
Generate random keys and construct the client
The first step is to generate random keys needed for the client and construct the client instance.
Rust
let keys: Keys = Keys::generate();
let client = Client::new(keys);
Python
keys = Keys.generate()
client = Client(keys)
JavaScript
let keys: Keys = Keys.generate();
let signer = NostrSigner.keys(keys);
let client = new Client(signer);
Kotlin
val keys = Keys.generate()
val signer = NostrSigner.keys(keys)
val client = Client(signer = signer)
Swift
let keys = Keys.generate()
let signer = NostrSigner.keys(keys: keys)
let client = Client(signer: signer)
Flutter
TODO
Add some relays and connect
Next, add some relays to your client and connect to them.
Rust
client.add_relay("wss://relay.damus.io").await?;
client.connect().await;
Python
await client.add_relay("wss://relay.damus.io")
await client.connect()
JavaScript
await client.addRelay("wss://relay.damus.io")
await client.connect();
Kotlin
client.addRelay("wss://relay.damus.io")
client.connect()
Swift
try await client.addRelay(url: "wss://relay.damus.io")
await client.connect()
Flutter
TODO
Publish a text note
Now that the client is constructed and the relays are connected, build a text note with the EventBuilder and publish it to relays.
Rust
let builder = EventBuilder::text_note("Hello, rust-nostr!");
let output = client.send_event_builder(builder).await?;
Python
builder = EventBuilder.text_note("Hello, rust-nostr!")
res = await client.send_event_builder(builder)
JavaScript
let builder = EventBuilder.textNote("Hello, rust-nostr!");
let res = await client.sendEventBuilder(builder);
Kotlin
val builder = EventBuilder.textNote("Hello, rust-nostr!")
val res = client.sendEventBuilder(builder)
Swift
let builder = EventBuilder.textNote(content: "Hello, rust-nostr!")
let res = try await client.sendEventBuilder(builder: builder)
Flutter
TODO
Inspect the output
Published the event, you can inspect the output to ensure everything worked correctly.
Rust
println!("Event ID: {}", output.id().to_bech32()?);
println!("Sent to: {:?}", output.success);
println!("Not sent to: {:?}", output.failed);
Python
print(f"Event ID: {res.id.to_bech32()}")
print(f"Sent to: {res.output.success}")
print(f"Not send to: {res.output.failed}")
JavaScript
console.log("Event ID:", res.id.toBech32());
console.log("Sent to:", res.output.success);
console.log("Not sent to:", res.output.failed);
Kotlin
println("Event ID: ${res.id.toBech32()}")
println("Sent to: ${res.output.success}")
println("Not sent to: ${res.output.failed}")
Swift
print("Event ID: \(try res.id.toBech32())")
print("Sent to: \(res.output.success)")
print("Not sent to: \(res.output.failed)")
Flutter
TODO
Full example
Here’s the full example that includes all the steps from generating keys to inspecting the output.
Rust
use nostr_sdk::prelude::*;
pub async fn hello() -> Result<()> {
let keys: Keys = Keys::generate();
let client = Client::new(keys);
client.add_relay("wss://relay.damus.io").await?;
client.connect().await;
let builder = EventBuilder::text_note("Hello, rust-nostr!");
let output = client.send_event_builder(builder).await?;
println!("Event ID: {}", output.id().to_bech32()?);
println!("Sent to: {:?}", output.success);
println!("Not sent to: {:?}", output.failed);
Ok(())
}
Python
from nostr_sdk import Keys, Client, EventBuilder
async def hello():
keys = Keys.generate()
client = Client(keys)
await client.add_relay("wss://relay.damus.io")
await client.connect()
builder = EventBuilder.text_note("Hello, rust-nostr!")
res = await client.send_event_builder(builder)
print(f"Event ID: {res.id.to_bech32()}")
print(f"Sent to: {res.output.success}")
print(f"Not send to: {res.output.failed}")
JavaScript
import {Keys, Client, EventBuilder, NostrSigner} from "@rust-nostr/nostr-sdk";
export async function hello() {
let keys: Keys = Keys.generate();
let signer = NostrSigner.keys(keys);
let client = new Client(signer);
await client.addRelay("wss://relay.damus.io")
await client.connect();
let builder = EventBuilder.textNote("Hello, rust-nostr!");
let res = await client.sendEventBuilder(builder);
console.log("Event ID:", res.id.toBech32());
console.log("Sent to:", res.output.success);
console.log("Not sent to:", res.output.failed);
}
Kotlin
import rust.nostr.sdk.*
suspend fun hello() {
val keys = Keys.generate()
val signer = NostrSigner.keys(keys)
val client = Client(signer = signer)
client.addRelay("wss://relay.damus.io")
client.connect()
val builder = EventBuilder.textNote("Hello, rust-nostr!")
val res = client.sendEventBuilder(builder)
println("Event ID: ${res.id.toBech32()}")
println("Sent to: ${res.output.success}")
println("Not sent to: ${res.output.failed}")
}
Swift
import Foundation
import NostrSDK
func hello() async throws {
let keys = Keys.generate()
let signer = NostrSigner.keys(keys: keys)
let client = Client(signer: signer)
try await client.addRelay(url: "wss://relay.damus.io")
await client.connect()
let builder = EventBuilder.textNote(content: "Hello, rust-nostr!")
let res = try await client.sendEventBuilder(builder: builder)
print("Event ID: \(try res.id.toBech32())")
print("Sent to: \(res.output.success)")
print("Not sent to: \(res.output.failed)")
}
Flutter
TODO