Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

guide: Django

Kiran K3989b05de76f
diff --git a/apps/docs/quickstart/django.mdx b/apps/docs/quickstart/django.mdx
index 57d65a4a5f..0447d47fc4 100644
--- a/apps/docs/quickstart/django.mdx
+++ b/apps/docs/quickstart/django.mdx
@@ -15,22 +15,140 @@ To install the Dub Python SDK, run the following command:
pip install dub
```
+initialize the Dub Python SDK by creating a new instance of the `Dub` class.
+
+```python
+import dub
+import os
+from dub.models import operations
+from django.http import JsonResponse
+
+d = dub.Dub(
+ token=os.environ['DUB_API_KEY'],
+ workspace_id='ws_clugls3...',
+)
+```
+
## 3. Create link
+Let's create a short link using the Dub Python SDK.
+
+```python views.py
+def create_link(request):
+ res = d.links.create(request=operations.CreateLinkRequestBody(
+ url='https://google.com',
+ ))
+
+ return JsonResponse({
+ 'short_link': res.link_schema.short_link
+ })
+```
+
+<Accordion title="Full list of available attributes for the Link model">
+ <LinkArguments />
+</Accordion>
+
+## 3a. Create link with `externalId`
+
+Optionally, you can also pass an `externalId` field which is a unique identifier for the link in your own database to associate it with the link in Dub's system.
+
+```python views.py
+def create_link(request):
+ res = d.links.create(request=operations.CreateLinkRequestBody(
+ url='https://google.com',
+ external_id='12345',
+ ))
+
+ return JsonResponse({
+ 'short_link': res.link_schema.short_link
+ })
+```
+
## 4. Upsert link
+Dub Python SDK provides a method to create or update a link so you don't have to worry about checking if the link already exists.
+
+```python views.py
+def upsert_link(request):
+ res = d.links.upsert(request=operations.UpsertLinkRequestBody(
+ url='https://google.com',
+ ))
+
+ return JsonResponse({
+ 'short_link': res.link_schema.short_link
+ })
+```
+
## 5. Update link
+Let's update an existing link using the Dub Node.js SDK.
+
+You can do that in two ways:
+
+- Using the link's `linkId` in Dub's system.
+- Using the link's `externalId` in your own database (prefixed with `ext_`).
+
+```python views.py
+# Update a link by its linkId
+def update_link(request):
+ res = d.links.update(link_id="clx1gvi9o0005hf5momm6f7hj", request_body=operations.UpdateLinkRequestBody(
+ url='https://google.uk',
+ ))
+
+ return JsonResponse({
+ 'short_link': res.link_schema.short_link
+ })
+
+# Update a link by its externalId
+def update_link_external(request):
+ res = d.links.update(external_id="ext_12345", request_body=operations.UpdateLinkRequestBody(
+ url='https://google.uk',
+ ))
+
+ return JsonResponse({
+ 'short_link': res.link_schema.short_link
+ })
+```
+
## 6. Retrieve analytics for link
+Dub allows you to retrieve analytics for a link using the Dub Python SDK.
+
+```python views.py
+# Retrieve the timeseries analytics for the last 7 days for a link
+def analytics(request):
+ res = d.analytics.retrieve(request=operations.RetrieveAnalyticsRequest(
+ link_id="clx1gvi9o0005hf5momm6f7hj",
+ interval="7d",
+ group_by="timeseries",
+ ))
+
+ return JsonResponse(res)
+```
+
+Simliarly, you can retrieve analytics for a link using the `externalId` field.
+
+```python views.py
+# Retrieve the timeseries analytics for the last 7 days for a link
+def analytics(request):
+ res = d.analytics.retrieve(request=operations.RetrieveAnalyticsRequest(
+ external_id="ext_12345",
+ interval="7d",
+ group_by="timeseries",
+ ))
+
+ return JsonResponse(res)
+```
+
## 7. Examples
<CardGroup cols={2}>
<Card
- title="Django Example"
+ title="Python Example"
icon="arrow-up-right-from-square"
- href="https://github.com/dubinc/examples"
+ href="https://github.com/dubinc/examples/tree/main/python/django"
>
See the full example on GitHub.
</Card>
</CardGroup>
+```