Nostr4j

A Java library for Nostr apps on desktop, mobile and the web.

  • Linux
  • Windows
  • Mac
  • Android
  • iOS
  • Browser
NGEPlatform.set(new JVMAsyncPlatform());

NostrKeyPair keys = new NostrKeyPair();
NostrPool pool = new NostrPool();
NostrSubscription subscription = null;
try {
    pool.ensureRelay("wss://relay.ngengine.org").await();

    NostrFilter filter = new NostrFilter();
    filter.withKind(1);
    filter.withAuthor(keys.getPublicKey());

    subscription = pool.subscribe(filter);
    subscription.addEventListener((sub, event, stored) -> {
        System.out.println("Received: " + event.getContent());
    });
    AsyncTask.awaitAll(subscription.open());

    UnsignedNostrEvent draft = new UnsignedNostrEvent();
    draft.withKind(1);
    draft.withContent("Hello from Nostr4j!");

    NostrKeyPairSigner signer = new NostrKeyPairSigner(keys);
    SignedNostrEvent note = signer.sign(draft).await();
    pool.publish(note).await();
    System.out.println("Published: " + note.getId());

    // Keep this standalone program alive briefly to receive the note.
    Thread.sleep(5_000);
} finally {
    if (subscription != null) subscription.close();
    pool.close();
    keys.close();
}

Add it to a project

See platform setup for dependencies and initialization details.

Linux, Windows and macOS use the same Java 21+ dependencies.

repositories {
    mavenCentral()
    maven { url = uri('https://central.sonatype.com/repository/maven-snapshots') }
}

dependencies {
    implementation 'org.ngengine:nostr4j:0.4.1'
    implementation 'org.ngengine:nge-platform-jvm:0.3.0-SNAPSHOT'
}
import org.ngengine.platform.NGEPlatform;
import org.ngengine.platform.jvm.JVMAsyncPlatform;

NGEPlatform.set(new JVMAsyncPlatform());