Skip to main content

bn-shortener (Python)

Official Python client for the BN Shortener API (b1.lc): short links, QR codes, bio pages, analytics, Launch Check, webhooks and OAuth. No dependencies; Python 3.9+.

pip install bn-shortener

Quick start

from bn_shortener import BNShortener

bn = BNShortener("bn_live_...")          # or set BN_API_KEY

link = bn.links.create(url="https://example.com/spring-sale", title="Spring sale")
print(link["short_url"])                 # https://b1.lc/AbC9

stats = bn.links.stats(link["id"], period="30d")
print(stats["summary"]["total_clicks"])

for l in bn.paginate(bn.links.list, search="sale"):   # every page
    print(l["short_code"], l["click_count"])

Create an API key in the dashboard: API Portal → API keys. A key has permissions (read, write, delete, webhooks, admin); give each integration only what it needs.

Resources

Attribute What
bn.links create, get, get_by_code, update, delete, archive, restore, clone, stats, clicks, referrers, devices, geo, timeline, export, import, bulk_create / bulk_update / bulk_delete, health, stream_clicks
bn.qr_codes create, get, update, delete, list, image (png, jpeg, webp, svg, pdf, eps), stats
bn.bio_pages create, get, update, delete, list, list_blocks, create_block, update_block, delete_block, reorder_blocks, analytics, subscribers, messages
bn.webhooks create, get, update, delete, list, test, deliveries, get_delivery, redeliver, events
bn.launch_check run, get, start_run, get_run, stop_run
bn.security scan, check, get_scan, bulk_scan, list_scans, ...
bn.messaging create_batch, list_batches, get_batch, list_messages, record_conversion
bn.api_keys, bn.account, bn.app_links, bn.system keys, usage + audit log, apps, API info

Every method's docstring names its endpoint and the permission it needs. Methods return the response's data; list methods return a Page (iterate it; page.pagination, page.next_page()); files come back as BinaryResponse (.content, .content_type, .save(path)):

qr = bn.qr_codes.create(name="Menu", content={"url": "https://example.com/menu"})
bn.qr_codes.image(qr["id"], format="svg", size=1000).save("menu.svg")

Command line

The package installs the bn command: bn login, bn shorten URL, bn links, bn stats CODE, bn check CODE (Launch Check, exit 1 when not ready), bn qr URL -o file.svg, bn bulk file.csv, bn api GET /usage. Run bn --help. The same command is a single file at https://b1.lc/cli/bn.

Errors

from bn_shortener import ValidationError, RateLimitError, NotFoundError, APIError

try:
    bn.links.create(url="not a url")
except ValidationError as e:
    print(e.errors)        # [{'field': 'url', 'code': ..., 'message': ...}]
except APIError as e:
    print(e.status, e.type, e.detail, e.request_id)   # e.type links to https://b1.lc/docs/errors/...

Connection errors, timeouts, 408, 429 and 5xx are retried (max_retries=2, waiting as long as Retry-After says). Every write carries an Idempotency-Key, so a retry after a lost answer never creates a second link. Pass request_options={"idempotency_key": "..."} to make your own retries safe too.

Apps that act for other people (OAuth 2.1)

from bn_shortener import BNShortener
from bn_shortener.oauth import OAuthClient, TokenSession

app = OAuthClient("bnc_...", redirect_uri="https://example.com/callback")
auth = app.authorization_url(scope=["read", "write"])     # keep auth.state + auth.code_verifier in the session
# send the person to auth.url; b1.lc sends them back to your redirect URI
code = app.parse_callback(callback_url, expected_state=auth.state)
token = app.exchange_code(code, auth.code_verifier)

bn = BNShortener(oauth=TokenSession(app, token, on_refresh=save_token))   # refreshes by itself

Command-line tools without a browser use the device flow:

device = app.start_device_flow(scope=["read", "write"])
print(f"Open {device.verification_uri} and type {device.user_code}")
token = app.wait_for_device(device)

Refresh tokens rotate: save the new token in on_refresh every time.

Webhooks

from bn_shortener.webhooks import construct_event

event = construct_event(request.get_data(), request.headers, WEBHOOK_SECRET)  # raises WebhookVerificationError

Pass the raw body, before any JSON parsing. Signatures older than 5 minutes are refused (replays).

Options

BNShortener(api_key, access_token=None, oauth=None, base_url="https://b1.lc/api/v1", timeout=30.0, max_retries=2, client_name=None, default_headers=None, transport=None). client_name shows your integration by name in the account's usage (GET /usage). bn.last_response has the status, request id and rate limit of the latest call. bn.request("GET", "/path") calls an endpoint this version does not wrap yet.

License

MIT

Metadata

Release files for bn-shortener 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bn-shortener 0.1.0
File Size Uploaded Provenance Actions
bn_shortener-0.1.0.tar.gz 44.2 kB Without provenance Details for bn_shortener-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bn-shortener 0.1.0
File Interpreter ABI Platform Actions
bn_shortener-0.1.0-py3-none-any.whl Python 3 none any Details for bn_shortener-0.1.0-py3-none-any.whl

Total release size: 91.0 kB

Release files / bn_shortener-0.1.0.tar.gz

Download URL bn_shortener-0.1.0.tar.gz
Size 44.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0765eef462af9e5094289c42c21c5a2a2103d58cdcf8c7bb4b6865973b86f58a
BLAKE2b-256 checksum
How to use checksums
46f47f23552a7b46beb68ea85385663fd571a51b1bddeff5ea7be1f382931223
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / bn_shortener-0.1.0-py3-none-any.whl

Download URL bn_shortener-0.1.0-py3-none-any.whl
Size 46.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aad97c103ad323b7fd24ce14019968e0aed2a89ff362493adbe289f7f4a588c6
BLAKE2b-256 checksum
How to use checksums
34723066393f1b11fa9e98f117879d48f9720d39e3011a0da3c0fcca1ae3accb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for bn-shortener 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page