Dub.co Python SDK
Learn more about the Dub.co Python SDK in the official documentation.
Summary
Dub API: Dub is the modern link attribution platform for short links, conversion tracking, and affiliate programs.
Table of Contents
SDK Installation
Note
Python version upgrade policy
Once a Python version reaches its official end of life date, a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated.
The SDK can be installed with uv, pip, or poetry package managers.
uv
uv is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools. It's recommended for its speed and modern Python tooling capabilities.
PIP
PIP is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
Poetry
Poetry is a modern tool that simplifies dependency management and package publishing by using a single pyproject.toml file to handle project metadata and dependencies.
Shell and script usage with uv
You can use this SDK in a Python shell with uv and the uvx command that comes with it like so:
It's also possible to write a standalone Python script without needing to set up a whole project like so:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "dub",
# ]
# ///
from dub import Dub
sdk = Dub(
# SDK arguments
)
# Rest of script here...
Once that is saved to a file, you can run it with uv run script.py where
script.py can be replaced with the actual file name.
SDK Example Usage
Example 1
# Synchronous Example
from dub import Dub
with Dub(
token="DUB_API_KEY",
) as d_client:
res = d_client.links.create(request={
"url": "https://google.com",
"external_id": "123456",
"tag_ids": [
"clux0rgak00011...",
],
"test_variants": [
{
"url": "https://example.com/variant-1",
"percentage": 50.0,
},
{
"url": "https://example.com/variant-2",
"percentage": 50.0,
},
],
})
# Handle response
print(res)
The same SDK client can also be used to make asynchronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from dub import Dub
async def main():
async with Dub(
token="DUB_API_KEY",
) as d_client:
res = await d_client.links.create_async(request={
"url": "https://google.com",
"external_id": "123456",
"tag_ids": [
"clux0rgak00011...",
],
"test_variants": [
{
"url": "https://example.com/variant-1",
"percentage": 50.0,
},
{
"url": "https://example.com/variant-2",
"percentage": 50.0,
},
],
})
# Handle response
print(res)
asyncio.run(main())
Example 2
# Synchronous Example
from dub import Dub
with Dub(
token="DUB_API_KEY",
) as d_client:
res = d_client.links.upsert(request={
"url": "https://google.com",
"external_id": "123456",
"tag_ids": [
"clux0rgak00011...",
],
"test_variants": [
{
"url": "https://example.com/variant-1",
"percentage": 50.0,
},
{
"url": "https://example.com/variant-2",
"percentage": 50.0,
},
],
})
# Handle response
print(res)
The same SDK client can also be used to make asynchronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from dub import Dub
async def main():
async with Dub(
token="DUB_API_KEY",
) as d_client:
res = await d_client.links.upsert_async(request={
"url": "https://google.com",
"external_id": "123456",
"tag_ids": [
"clux0rgak00011...",
],
"test_variants": [
{
"url": "https://example.com/variant-1",
"percentage": 50.0,
},
{
"url": "https://example.com/variant-2",
"percentage": 50.0,
},
],
})
# Handle response
print(res)
asyncio.run(main())
Available Resources and Operations
Available methods
- retrieve - Retrieve analytics for a link, a domain, or the authenticated workspace.
- list - List all customers
- get - Retrieve a customer
- delete - Delete a customer
- update - Update a customer
- list - List discount codes
- create - Create a discount code
- delete - Delete a discount code