A bash script for fetching all notes from a specific Nostr public key and saving them as individual JSON files.
This script connects to a Nostr relay via WebSocket, subscribes to receive all notes from a specified public key, and saves each note as a separate JSON file. It's useful for archiving, analyzing, or backing up Nostr content.
- Bash shell environment
websocat
- WebSocket client tooljq
- JSON processor
- Download the script file
- Make the script executable:
chmod +x fetch_nostr_notes.sh
Basic usage:
./fetch_nostr_notes.sh <relay_websocket_url> <pubkey> [output_directory]
Parameters
* relay_websocket_url: WebSocket URL of the Nostr relay (e.g., wss://relay.damus.io)
* pubkey: The Nostr public key in hex format to fetch notes from
* output_directory: (Optional) Directory to save notes to (default: "nostr_notes")
Example
./fetch_nostr_notes.sh wss://relay.primal.net 9e30e940238cd9ebebc6328176dd4d109812129442f2a6c38727fc66fa7ea90a my_notes
This will:
Connect to the relay at wss://relay.primal.net
Request all notes from @lontivero's public key 9e30e940238cd9ebebc6328176dd4d109812129442f2a6c38727fc66fa7ea90a
Save them as individual JSON files in the "my_notes" directory
Output
The script creates a directory (default: "nostr_notes" if not specified) and saves each note as a separate JSON file.
Each file:
Is named with the note's ID (e.g., a1b2c3d4e5f6.json)
Contains the complete JSON data of the note
Includes all metadata such as timestamp, tags, and signatures
The script also provides a running output showing:
When notes are saved
Timestamps of notes
Brief previews of note content
Error Handling
The script will exit with an error if the required parameters are not provided
Timestamps are converted to human-readable dates when possible
The script reports end-of-stored-events when the relay completes sending all available notes
Customization
You can modify the script to change:
The subscription parameters (e.g., add a time range by changing the REQ parameters)
The output format by modifying the jq commands
The maximum number of notes by adjusting the "limit" parameter in the subscription request
License
This script is provided under the MIT License. Contributing
Feel free to submit issues or pull requests with improvements or bug fixes.