mirror of
https://github.com/nostr-connect/connect.git
synced 2025-12-17 05:04:20 +01:00
52f6b6ced871c88eaba3445d7f9fd25097a8fa0a
🔌 Nostr Connect SDK for TypeScript
Nostr Connect SDK for TypeScript is a library that allows you to easily integrate Nostr Connect into your web application.
📦 Installation
You can install the SDK using npm or yarn:
npm install @nostr-connect/connect
# or with yarn
yarn add @nostr-connect/connect
📖 Usage
Create an ephemeral key
To use the SDK, you need to create an ephemeral key. This key is used to authenticate your user and to create a session.
import { generatePrivateKey } from 'nostr-tools';
const sk = generatePrivateKey();
Create a Nostr Connect instance
To create a Nostr Connect instance, you need to provide the ephemeral key and the Nostr Connect URL.
import { Connect } from '@nostr-connect/connect';
const connect = new Connect({ secretKey: sk, relay: 'wss://nostr.vulpem.com' });
connect.events.on('connect', ( walletPubkey:string ) => {
console.log('connected with wallet: ' + walletPubkey);
});
await connect.init();
Generate a ConnectURI and display it to the user
const connectURI = new ConnectURI({
target: webPK,
relayURL: 'wss://nostr.vulpem.com',
metadata: {
name: 'My Website',
description: 'lorem ipsum dolor sit amet',
url: 'https://vulpem.com',
icons: ['https://vulpem.com/1000x860-p-500.422be1bc.png'],
},
});
const uri = connectURI.toString();
Start making requests
// send the get_public_key message to the mobile app
const pubkey = await connect.getPublicKey();
// send the sign_event message to the mobile app
const sig = await connect.signEvent(event);
Languages
TypeScript
98.2%
HTML
1.5%
JavaScript
0.3%