This is a simple Python wrapper for the LinkWarden API, it's still a work in progress, every contribution or suggestion is welcome!
# Clone the repository
git clone https://github.com/El3k0n/linkwarden.git
cd linkwarden
# Install dependencies
pip install -r requirements.txt
- Go to Linkwarden
- Sign in to your account
- Go to Settings → API Keys
- Create a new API key
- Copy the key
from linkwarden import Api
#If using Linkwarden cloud
api = Api(api_key="your-api-key-here")
#If using a self-hosted instance
api = Api(
api_key="your-api-key-here",
base_url="http://linkwarden.yourdomain.com"
)
#Get all the links for the current user
#NOTE: Endpoint deprecated in favor of search.search_links
links = api.links.get_links()
# Retrieve a link by ID
link = api.links.get_link(123)
print(f"Link: {link['name']}")
print(f"URL: {link['url']}")
print(f"Description: {link["description"]}")
# Get all links fields
link.keys()
# Create a simple link
new_link = api.links.create_link(
name="Google",
url="https://google.com",
type="url"
)
print(f"Link created with ID: {new_link['id']}")
print(f"Link created in collection: {new_link['collection']['id']} named {new_link['collection']['name']}")
# Create a link with tags and collection
new_link = api.links.create_link(
name="Important Document",
url="https://example.com/doc.pdf",
type="pdf",
description="Important work document",
tags=[{"id": 1, "name": "work"}],
collection={"id": 2, "name": "Work"}
)
# Update only some fields
updated_link = api.links.update_link(
id=123, #Required
name="New name", #Required
url="http://example.com", #Required
collection =Â { #All these collection fields are required
"id":0,
"name":"MyCollection",
"ownerId":1
}
description="New description"
)
# Archive a link
archived_link = api.links.archive_link(id=123)
print(archived_link)
# Delete a single link
api.links.delete_link(id=123)
# Delete multiple links
api.links.delete_link_list(ids=[123, 124, 125])
users = api.users.get_users()
for user in users:
print(f"ID: {user['id']}, Username: {user['username']}, Email: {user.get('email')}")
user = api.users.get_user(1)
print(f"User: {user['username']}")
new_user = api.users.create_user(
name="New User",
password="password123",
email="[email protected]",
username="newuser"
)
updated_user = api.users.update_user(
user_id=1,
name="Updated Name",
email="[email protected]"
)
api.users.delete_user(user_id=1)
tags = api.tags.get_tags()
for tag in tags:
print(f"Tag: {tag['name']}, Color: {tag['color']}")
tag = api.tags.get_tag(1)
print(f"Tag: {tag['name']}")
updated_tag = api.tags.update_tag(
tag_id=1,
name="New Tag Name"
)
api.tags.delete_tag(tag_id=1)
collections = api.collections.get_collections()
for collection in collections:
print(f"Collection: {collection['name']}")
collection = api.collections.get_collection(1)
print(f"Collection: {collection['name']}")
new_collection = api.collections.create_collection(
name="New Collection",
description="Collection description"
)
updated_collection = api.collections.update_collection(
id=1,
name="Updated Name"
)
api.collections.delete_collection(id=1)
dashboard = api.dashboard.get_current_user_dashboard()
print(f"Got {len(dashboard)} entries")
print(f"First entry: {dashboard[0]["name"]}")
# Returns binary image data
avatar_bytes = api.avatar.get_avatar(1)
# Save to file
with open("avatar.png", "wb") as f:
f.write(avatar_bytes)
result = api.search.search_links(query="My Search")
result_links = result['data']['links']
print(f"Found {len(result_links)} results")
tokens = api.tokens.get_tokens()
for token in tokens:
print(f"Token: {token['name']}, Created: {token['createdAt']}")
new_token = api.tokens.create_token(
name="New Token",
expires=1234567890 # Seconds to expire, 0 for never
)
print(f"Token created: {new_token['token']}")
api.tokens.revoke_token(token_id=1)
# Export all data
export_data = api.migration.export_data()
with open("export.json", "w") as f:
f.write(export_data)
# Import data from file
with open("export.json", "r") as f:
data = f.read()
import_response = api.migration.import_data(data)
print(f"Response: {import_response}")
archive = api.archives.get_archive_by_link_id(link_id=123, format=2) #Get PDF archive
# Formats: 0 = PNG, 1 = JPEG, 2 = PDF, 3 = JSON, 4 = HTML
with open("archive.pdf", "wb") as f:
f.write(archive)
# From disk file
result = api.archives.upload_file_to_archive(
link_id=123,
file_path="/path/to/file.pdf",
format="pdf"
)
# From file object
with open("file.pdf", "rb") as f:
result = api.archives.upload_file_object_to_archive(
link_id=123,
file_object=f,
format="pdf"
)
Public endpoints don't require authentication:
# Get links from public collection
public_links = api.public.get_links_from_collection(collection_id=1)
# Get tags from public collection
public_tags = api.public.get_tags_from_collection(collection_id=1)
# Get public user
public_user = api.public.get_user_by_id(user_id=1)
# Get public link
public_link = api.public.get_link_by_id(link_id=1)
# Get public collection
public_collection = api.public.get_collection_by_id(collection_id=1)