shrtnr
Python SDK for shrtnr, a self-hosted URL shortener on Cloudflare Workers. Create short links, manage slugs, and read click analytics.
Install
pip install shrtnr
Quick start
from shrtnr import Shrtnr
client = Shrtnr(base_url="https://your-shrtnr.example.com", api_key="sk_your_api_key")
link = client.links.create(url="https://example.com/very-long-path")
print(link.slugs[0].slug) # "a3x9"
Async:
import asyncio
from shrtnr import AsyncShrtnr
async def main():
async with AsyncShrtnr(base_url="https://your-shrtnr.example.com", api_key="sk_...") as client:
link = await client.links.create(url="https://example.com")
print(link.id)
asyncio.run(main())
Configuration
Shrtnr(
base_url="https://your-shrtnr.example.com", # required
api_key="sk_...", # required; from the admin dashboard
timeout=30.0, # optional; seconds (default: 30)
http_client=custom_httpx_client, # optional; inject a custom httpx.Client
)
AsyncShrtnr takes the same parameters, with an httpx.AsyncClient for http_client. Both classes work as context managers:
with Shrtnr(base_url="...", api_key="sk_...") as client:
links = client.links.list()
Resources
Links (client.links)
| Method | Description |
|---|---|
get(id, *, range=None) |
Get a link with click count |
list(*, owner=None, range=None) |
List all links |
create(*, url, label=None, slug_length=None, expires_at=None, allow_duplicate=None) |
Create a short link |
update(id, *, url=None, label=UNSET, expires_at=UNSET) |
Update URL, label, or expiry. Omit a field to leave it unchanged; pass None to clear it |
disable(id) |
Stop redirecting |
enable(id) |
Resume redirecting |
delete(id) |
Permanently delete |
analytics(id, *, range=None) |
Click breakdown by country, device, referrer, etc. |
breakdown(id, *, dimension, range=None, offset=None, limit=None) |
Page through the countries, sources (referrers) or domains (referrer_hosts) breakdown |
timeline(id, *, range=None) |
Click counts bucketed over time |
qr(id, *, slug=None, size=None) |
QR code as SVG string |
bundles(id) |
Bundles this link belongs to |
# Shorten a URL
link = client.links.create(url="https://example.com", label="Landing page")
# Get a 7-day click count
fresh = client.links.get(link.id, range="7d")
# Full analytics for the last 30 days
stats = client.links.analytics(link.id, range="30d")
print(stats.total_clicks, stats.countries, stats.browsers)
Slugs (client.slugs)
| Method | Description |
|---|---|
lookup(slug) |
Find a link by slug |
add(link_id, slug) |
Add a custom slug |
disable(link_id, slug) |
Disable a slug |
enable(link_id, slug) |
Re-enable a slug |
remove(link_id, slug) |
Remove a slug |
# Add a campaign slug then disable it when the campaign ends
client.slugs.add(link.id, "spring-sale")
client.slugs.disable(link.id, "spring-sale")
# Look up a link by its slug
found = client.slugs.lookup("spring-sale")
Bundles (client.bundles)
Groups of related links with combined analytics.
| Method | Description |
|---|---|
get(id, *, range=None) |
Get a bundle with click summary |
list(*, archived=None, range=None) |
List bundles |
create(*, name, description=None, icon=None, accent=None) |
Create a bundle |
update(id, *, name=None, description=UNSET, icon=UNSET, accent=None) |
Update metadata. Omit a field to leave it unchanged; pass None to clear description or icon |
delete(id) |
Permanently delete |
archive(id) |
Hide from default listing |
unarchive(id) |
Restore an archived bundle |
analytics(id, *, range=None) |
Combined click analytics |
breakdown(id, *, dimension, range=None, offset=None, limit=None) |
Page through the countries, sources (referrers) or domains (referrer_hosts) breakdown |
links(id) |
List links in the bundle |
add_link(id, link_id) |
Add a link |
remove_link(id, link_id) |
Remove a link |
# Create a bundle and add links to it
bundle = client.bundles.create(name="Spring 2026", accent="green")
client.bundles.add_link(bundle.id, link_a.id)
client.bundles.add_link(bundle.id, link_b.id)
# Combined analytics for the last 7 days
stats = client.bundles.analytics(bundle.id, range="7d")
print(stats.total_clicks)
Config (client.config)
Deployment settings a client needs to build short URLs.
| Method | Description |
|---|---|
get() |
Origin short URLs are handed out on |
# Matches the copy button and QR code: primary slug, else first custom, else generated.
# link.short_origin works on deployments that predate config.get().
primary = (
next((s for s in link.slugs if s.is_primary), None)
or next((s for s in link.slugs if s.is_custom), None)
or link.slugs[0]
)
short_url = f"{link.short_origin}/{primary.slug}"
Models
Model fields use snake_case, matching the wire format. Types are frozen dataclasses.
Every Link carries short_origin: the origin this deployment hands short URLs out on. It is the deployment's SHORT_ORIGIN when set, otherwise the request origin. client.config.get() returns the same value. Build short URLs as f"{short_origin}/{slug}" so they match the copy button, the QR code and the redirects. Deployments older than the field omit it, so the SDK fills in the origin of base_url.
Key types exported from shrtnr:
Link,Slug,Bundle,BundleWithSummary,BundleTopLinkConfigClickStats,TimelineData,TimelineBucket,NameCount,TimelineSummaryDateCount,SlugCountDeletedResult,AddedResult,RemovedResultTimelineRange(Literal["24h", "7d", "30d", "90d", "1y", "all"])BreakdownDimension,BreakdownPageBundleAccent(Literal["orange", "red", "green", "blue", "purple"])
Errors
Any 4xx/5xx response or network failure raises ShrtnrError. A network failure carries status=0.
from shrtnr import ShrtnrError
try:
client.links.get(99999)
except ShrtnrError as err:
print(err.status) # 404
print(err.server_message) # "not found"
print(str(err)) # "shrtnr API error (HTTP 404): not found"
See also
- API docs:
/_/api/docson your shrtnr deployment - OpenAPI spec:
/_/api/openapi.json - Source: github.com/oddbit/shrtnr
Attribution
shrtnr is developed and maintained by Oddbit.
- Source repository: https://github.com/oddbit/shrtnr
- License: Apache License 2.0
- Attribution notices: NOTICE
- Name and logo usage: Trademark Policy
If you publish a fork or derivative work, retain the license and notice files, preserve applicable copyright and attribution notices, and clearly indicate that your version has been modified.
Metadata
Release files for shrtnr 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| shrtnr-1.3.0.tar.gz | 22.2 kB | With provenance | Details for shrtnr-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| shrtnr-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for shrtnr-1.3.0-py3-none-any.whl |
Total release size: 45.0 kB
Release files / shrtnr-1.3.0.tar.gz
| Download URL | shrtnr-1.3.0.tar.gz |
|---|---|
| Size | 22.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ee64d47c2113426909cd2742b944d7a9393f3b91e065764b166f5e83113cbcce
|
|
BLAKE2b-256 checksum How to use checksums |
6b2a383d481ec2c0f72387b636396971baa58baa6b48e0fcd5a54bb57ac419fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.
Transparency logRelease files / shrtnr-1.3.0-py3-none-any.whl
| Download URL | shrtnr-1.3.0-py3-none-any.whl |
|---|---|
| Size | 22.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bf7da856b6cab140d4035db93799be17b5c5781323c4a95fd5f34cb9fce59d8b
|
|
BLAKE2b-256 checksum How to use checksums |
05b2c3f4a7c06f5627119240b1056d542466cf35d8db688ce339909c83de776e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.
Transparency logSecurity for shrtnr 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for shrtnr-1.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.