ever4st
ever4st
alashazam@iris.to
Feb 21, 2025

Nostr_sdk: Writing metadata

using Nostr_sdk to write and read user metadata
#!/usr/bin/env python3
import asyncio
from nostr_sdk import Metadata, Client, NostrSigner, Keys, Filter, PublicKey, Kind, init_logger, LogLevel
from datetime import timedelta

async def main():
    init_logger(LogLevel.INFO)
    secret_key = "nsec1........Replace with your actual nsec secret key"
    keys = Keys.parse(secret_key)
    signer = NostrSigner.keys(keys)
    client = Client(signer)
    
    await client.add_relay("wss://relay.damus.io")
    await client.connect()

    # Update metadata
    new_metadata = Metadata().set_name( "MyName")\
    .set_nip05("MyName@example.com")\
    .set_lud16("MyName@lud16.com")
    await client.set_metadata(new_metadata)
    print("Metadata updated successfully.")
    # Get updated metadata
    npub = "npub1....Replace with your actual npub"
    pk = PublicKey.parse(npub)
    print(f"\nGetting profile metadata for {npub}:")
    metadata = await client.fetch_metadata(pk, timedelta(seconds=15))
    print(metadata)
        
if __name__ == '__main__':
    asyncio.run(main())