Complete, runnable examples for common use cases with the Linkbreakers API.
# Install the SDK
pip install linkbreakers
# Set your API key
export LINKBREAKERS_API_KEY='your-api-key-here'Or create a .env file (see .env.example):
LINKBREAKERS_API_KEY=your-api-key-hereEach example can be run directly with Python:
python examples/identify_visitor.pyMost Important Use Case - Identify or create a visitor using their LBID from tracking.
# Find or create visitor, merge attributes
response = visitors_api.visitors_service_identify(
identify_request=IdentifyRequest(
lbid='visitor-lbid-from-tracking',
visitor=VisitorInput(
data={
'$email': 'user@example.com',
'$phone': '+1234567890',
'company': 'Acme Corp',
'plan': 'premium'
}
)
)
)When to use:
- User signs up or logs in
- User fills out a form
- Capturing visitor information from tracking events
- First-time visitor identification
Key concepts:
- Uses LBID (base64 encoded event ID from click/scan)
- Creates visitor if doesn't exist, updates if exists
- System fields use
$prefix ($email,$phone,$firstName,$lastName) - Custom attributes have no prefix
set_once=Trueprevents overwriting existing data
Update an existing visitor using their UUID.
# Update visitor by UUID
visitor = visitors_api.visitors_service_update(
id='visitor-uuid',
visitors_service_update_body=VisitorsServiceUpdateBody(
visitor=VisitorInput(
data={
'$email': 'updated@example.com',
'plan': 'enterprise'
}
)
)
)When to use:
- User updates their profile
- Subscription changes
- Enriching data from external sources
- You have the visitor UUID from your database
Includes:
- Single visitor update
- Batch update multiple visitors
Query, filter, and search visitors.
# List with filters
visitors = visitors_api.visitors_service_list(
page_size=50,
email='user@example.com',
link_id='link-uuid',
search='Acme Corp'
)Features:
- Pagination through results
- Filter by email (exact match)
- Filter by link ID
- Fuzzy search across fields
- Export to CSV
- Include related data (devices, events, links)
Create shortened links with various configurations.
# Basic link
link = links_api.links_service_create(
create_link_request=CreateLinkRequest(
destination='https://example.com',
name='My Campaign'
)
)
# With custom shortlink, tags, and metadata
custom_link = links_api.links_service_create(
create_link_request=CreateLinkRequest(
destination='https://example.com/sale',
shortlink='summer2024',
tags=['campaign', 'summer'],
metadata={
'campaign_id': 'SUMMER_2024'
}
)
)Includes examples for:
- Basic shortened links
- Custom shortlinks
- Tags and metadata
- QR code generation
- Custom domains
- Bulk link creation
-
LBID (Linkbreakers ID): Base64 encoded event ID from click/scan tracking
- Comes from tracking cookies, query parameters, or webhooks
- Used with
identifyendpoint - Format:
ZXhhbXBsZS1saW5rYnJlYWtlcnMtaWQtMTIzNDU2Nzg5MA==
-
UUID: Standard visitor identifier stored in your database
- Returned from API responses
- Used with
update,get,deleteendpoints - Format:
550e8400-e29b-41d4-a716-446655440000
System fields (prefixed with $):
$email- Email address$phone- Phone number$firstName- First name$lastName- Last name
Custom attributes (no prefix):
- Store any data you need:
company,plan,signupDate, etc. - Used for segmentation, personalization, and analytics
Most list endpoints support pagination:
page_token = None
while True:
response = api.list(
page_size=200,
page_token=page_token
)
# Process results
page_token = response.next_page_token
if not page_token:
breaktry:
visitor = visitors_api.visitors_service_identify(
identify_request=IdentifyRequest(...)
)
print('Success:', visitor)
except ApiException as error:
print(f'API Error: {error.status} - {error.reason}')
except Exception as error:
print(f'Error: {error}')import os
from linkbreakers import Configuration, ApiClient
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY'),
host=os.getenv('LINKBREAKERS_API_URL', 'https://api.linkbreakers.com')
)
with ApiClient(configuration) as api_client:
# Use api_client
passAlways use the with statement for ApiClient to ensure proper resource cleanup:
with ApiClient(configuration) as api_client:
api = SomeApi(api_client)
result = api.some_method()
# Resources are automatically cleaned up- API Documentation: https://linkbreakers.com/help/api
- SDK Issues: https://github.com/linkbreakers-com/linkbreakers-python/issues
- Main README: ../README.md
Have a useful example? Submit a PR!
- Create a new file in
examples/ - Include clear docstrings and comments
- Make it runnable with realistic fake data
- Add it to this README