High-level Python client for the Blossom protocol (blob storage via Nostr auth)
Project description
python-blossom
High-level Python client for the Blossom protocol (blob storage via Nostr auth).
Implements core BUDs:
- BUD-01: Blob retrieval (GET/HEAD)
- BUD-02: Upload, List, Delete
- BUD-03: User Server List event generation (kind 10063)
- BUD-04: Mirror blob
- BUD-05: Media optimization (optional endpoints)
- BUD-06: Upload requirements (HEAD /upload)
Installation
Install from PyPI (coming soon):
pip install python-blossom
Or install locally with pipenv:
pipenv install
For development:
pipenv install --dev
Quick Start
from blossom_python import BlossomClient
nsec = 'nsec....' # Your Nostr private key (nsec)
servers = [
'https://blossom.band',
'https://nostr.download',
'https://blossom.primal.net'
]
client = BlossomClient(nsec=nsec, default_servers=servers)
# Upload blob data
result = client.upload_blob(servers[0], data=b'blob data', mime_type='image/png')
sha256 = result['sha256']
print(f"Uploaded: {result['url']}")
# Get blob
blob = client.get_blob(servers[0], sha256, mime_type='image/png')
downloaded_data = blob.get_bytes()
print(f"Downloaded: {len(downloaded_data)} bytes")
# HEAD request to check blob metadata
meta = client.head_upload_requirements(servers[0], data=b'blob data', mime_type='image/png')
print(f"Server supports: {meta}")
# List user's blobs
blobs = client.list_blobs(servers[0], pubkey=client.pubkey_hex, use_auth=True)
print(f"Found {len(blobs)} blobs")
# Delete blob
client.delete_blob(servers[0], sha256, description='Cleanup')
# Mirror blob to another server
mirror_result = client.mirror_blob(servers[1], servers[0], sha256)
print(f"Mirrored to: {mirror_result['url']}")
# Generate User Server List event (kind 10063, BUD-03)
event = client.generate_server_list_event(servers=servers)
print(f"Event ID: {event['id']}")
# Publish server list event to relays
event_id = client.publish_server_list_event(relays=['wss://relay.damus.io'])
print(f"Published: {event_id}")
Publishing Media to Nostr with Redundancy
Here's how to upload a photo to multiple Blossom servers and publish it to Nostr with automatic failover:
from python_blossom import BlossomClient
from pynostr.event import Event
from pynostr.key import PrivateKey
import json
nsec = 'nsec....' # Your Nostr private key
servers = [
'https://blossom.band',
'https://nostr.download',
'https://blossom.primal.net'
]
client = BlossomClient(nsec=nsec, default_servers=servers)
# Step 1: Publish your server list to Nostr (do this once or when you change servers)
# This advertises all your Blossom servers to the Nostr network
# Clients use this to find backup servers if the primary one is down
relays = ['wss://relay.damus.io', 'wss://relay.snort.social']
client.publish_server_list_event(relays=relays, servers=servers)
print(f"✓ Published server list to relays")
# Step 2: Upload photo to ALL servers for redundancy
with open('photo.jpg', 'rb') as f:
photo_data = f.read()
upload_results = client.upload_to_all(
data=photo_data,
mime_type='image/jpeg',
description='My vacation photo'
)
# Get the primary URL (first successful upload)
primary_url = None
for server, result in upload_results.items():
if 'url' in result and not 'error' in result:
primary_url = result['url']
sha256 = result['sha256']
print(f"✓ Uploaded to {server}: {primary_url}")
break
# Step 3: Create Nostr note with media tag pointing to primary URL
# If the primary server goes down, clients will use your server list event
# (kind 10063) to find the photo on your other servers
private_key = PrivateKey.from_nsec(nsec)
event = Event(
content="Check out this photo! 📸",
kind=1 # Text note
)
# Add image tag with primary URL
# Format: ["image", url, "m" MIME type, "alt" description, "x" sha256]
event.add_tag("image", primary_url, "m", "image/jpeg", "alt", "My vacation photo", "x", sha256)
event.sign(private_key.hex())
# Publish to relays using pynostr
from pynostr.relay_manager import RelayManager
relay_manager = RelayManager(timeout=5)
for relay in relays:
relay_manager.add_relay(relay)
relay_manager.publish_event(event)
relay_manager.run_sync()
relay_manager.close_all_relay_connections()
API Methods
Upload & Download
upload_blob(server, data=None, file_path=None, mime_type=None, description=None, use_auth=False)
- Upload blob data (raw bytes or file)
- Auto-detects MIME type if not specified
- Returns blob descriptor with
sha256,url,size,type
get_blob(server, sha256, mime_type=None, use_auth=False)
- Download blob by SHA256 hash
- Returns
Blobobject withcontent,sha256,mime_typeproperties - Call
blob.get_bytes()orblob.save(file_path)to access data
head_upload_requirements(server, data=None, file_path=None, mime_type=None, use_auth=False)
- Check server upload requirements before uploading
- Returns headers:
content_type,content_length,accept_ranges
Media (BUD-05)
media_upload(server, data=None, file_path=None, mime_type=None, description=None, use_auth=False)
- Upload blob with media optimization
- Returns blob descriptor
media_head(server, data=None, file_path=None, mime_type=None, use_auth=False)
- Check media upload requirements
- Returns headers
Blob Management
upload_blob(server, data=None, file_path=None, mime_type=None, description=None, use_auth=True)
- Upload blob data (raw bytes or file)
- Auto-detects MIME type if not specified
- Returns blob descriptor with
sha256,url,size,type
upload_to_all(data=None, file_path=None, mime_type=None, description=None, use_auth=True)
- Upload blob to all default servers in parallel
- Uses servers configured during client initialization
- Returns dict mapping server URLs to results/errors
list_blobs(server, pubkey=None, cursor=None, limit=None, use_auth=False)
- List blobs for a public key (accepts npub or hex format)
- Returns list of blob descriptors
- Requires
use_auth=Truefor private server listings
delete_blob(server, sha256, description=None)
- Delete blob from server
- Requires authorization
- Returns confirmation
mirror_blob(server, source_url, sha256, description=None)
- Mirror blob from one server to another
- Returns blob descriptor on destination server
Head Blob
head_blob(server, sha256, use_auth=False)
- Get blob metadata without downloading content
- Returns headers:
content_type,content_length,accept_ranges
Server Management (BUD-03)
Server list events (Nostr kind 10063) are like "advertising" - they publicly announce which Blossom servers a user uploads media to. When you publish a server list event to the Nostr network, anyone can look up your public key and discover which servers you use. NOSTR clients use this to know where to look for published media in a NOSTR note.
generate_server_list_event(servers=None)
- Generate a Nostr server list event (kind 10063, BUD-03)
- Contains tags with your Blossom server URLs
- Automatically signed with your private key
- Returns Nostr event dict ready for publishing
publish_server_list_event(relays, servers=None)
- Publish your server list event to Nostr relays
- Advertises your Blossom servers publicly by your public key
relays: List of relay URLs (wss://) where the event will be publishedservers: Optional list of server URLs (defaults to configured servers on the client)- Returns event ID of the published event
fetch_server_list(relays, pubkey=None, timeout=2.0)
- Look up which Blossom servers a user has advertised
- Query Nostr relays for a user's server list event
relays: List of relay URLs to querypubkey: Optional public key in any format (npub or hex) to look up (defaults to your own)- Returns list of server URLs that user has advertised
Authorization
Methods with use_auth=True parameter automatically generate NIP-98 authorization events:
- kind 24242
- Tags:
t=<verb>,expiration,x=<hash>(when applicable) - Base64 encoded in
Authorization: Nostr <base64>header - Auto-signed with your private key
Error Handling
Raises BlossomError or specific subclasses on errors:
InvalidAuthorizationEvent: 401 auth failureBlobNotFound: 404 blob not foundTooManyRequests: 429 rate limitBlossomError: Other HTTP errors
from blossom_python.errors import BlobNotFound, TooManyRequests
try:
blob = client.get_blob(server, sha256)
except BlobNotFound:
print("Blob not found")
except TooManyRequests:
print("Rate limited - try again later")
TODO / Unsupported Endpoints
The following Blossom endpoints are not yet implemented:
- BUD-09: PUT
/report- Report abuse/misinformation (planned for future release)
License
Unlicense / Public Domain (matches upstream Blossom spec repository).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_blossom-1.0.2.tar.gz.
File metadata
- Download URL: python_blossom-1.0.2.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e68141108d928cf769ff4c86dd74b744b415b73c42072469a4c70b96a0c8086d
|
|
| MD5 |
5b79e0552f4d2cf3c5779fcfa56531d8
|
|
| BLAKE2b-256 |
93ee148731217cabe972d8e1252f099762719e0805b1f735e22f9edcd1bcf58c
|
File details
Details for the file python_blossom-1.0.2-py3-none-any.whl.
File metadata
- Download URL: python_blossom-1.0.2-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42e01049176f5fc05522e30245040db26cf766fb9613af19f1497254a27685e6
|
|
| MD5 |
4a0916be4ad71e8319bb7eb2962c83ce
|
|
| BLAKE2b-256 |
ab7e89b03deae731790654522c13a2981f22f7dc9269c7eeba472e20f8f168f2
|