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: python

Kiran Kbc1682c9eb64
diff --git a/apps/docs/quickstart/express.mdx b/apps/docs/quickstart/express.mdx
index 2aeed6d3ff..c17f3ae8c0 100644
--- a/apps/docs/quickstart/express.mdx
+++ b/apps/docs/quickstart/express.mdx
@@ -10,20 +10,34 @@ import InstallNode from "/snippets/sdk-install-nodejs.mdx";
<InstallNode />
-Create a dub instance using the Dub Node.js SDK.
+## 3. Create link
+
+Let's create a short link using the Dub Node.js SDK.
-```typescript dub.ts
-import { Dub } from "dub";
+```typescript index.ts
+import express, { Request, Response } from "express";
+import { dub } from "./dub";
-export const dub = new Dub({
- token: process.env.DUB_API_KEY,
- workspaceId: process.env.DUB_WORKSPACE_ID,
+app.get("/create-link", async (req: Request, res: Response) => {
+ try {
+ const result = await dub.links.create({
+ url: "https://www.google.com",
+ });
+
+ res.status(200).json(result);
+ } catch (error: any) {
+ res.status(400).json(error);
+ }
});
```
-## 3. Create link
+<Accordion title="Full list of available attributes for the Link model">
+ <LinkArguments />
+</Accordion>
-Let's create a short link using the Dub Node.js SDK.
+## 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.
```typescript index.ts
import express, { Request, Response } from "express";
@@ -33,6 +47,7 @@ app.get("/create-link", async (req: Request, res: Response) => {
try {
const result = await dub.links.create({
url: "https://www.google.com",
+ externalId: "12345",
});
res.status(200).json(result);
@@ -52,7 +67,6 @@ app.get("/upsert-link", async (req: Request, res: Response) => {
// Update the link if same URL already exists or create a new link
const result = await dub.links.upsert({
url: "https://www.google.com",
- externalId: "my-link-id",
});
res.status(200).json(result);
@@ -66,14 +80,25 @@ app.get("/upsert-link", async (req: Request, res: Response) => {
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_`).
+
```typescript index.ts
app.get("/update-link", async (req: Request, res: Response) => {
try {
- const result = await dub.links.update("LINK_ID", {
- url: "https://www.google.us",
+ // Update a link by its linkId
+ const { shortLink } = await dub.links.update("clv3o9p9q000au1h0mc7r6l63", {
+ url: "https://www.google.uk", // new URL
});
- res.status(200).json(result);
+ // Update a link by its externalId
+ const { shortLink } = await dub.links.update("ext_12345", {
+ url: "https://www.google.uk", // new URL
+ });
+
+ res.status(200).json({ shortLink });
} catch (error: any) {
res.status(400).json(error);
}
@@ -91,9 +116,8 @@ app.get("/analytics", async (req: Request, res: Response) => {
try {
// Retrieve the timeseries analytics for the last 7 days for a link
const response = await dub.analytics.retrieve({
- linkId: "LINK_ID",
+ linkId: "clv3o9p9q000au1h0mc7r6l63",
interval: "7d",
- event: "clicks",
groupBy: "timeseries",
});
@@ -106,6 +130,19 @@ app.get("/analytics", async (req: Request, res: Response) => {
});
```
+Simliarly, you can retrieve analytics for a link using the `externalId` field.
+
+```typescript index.ts
+// Retrieve the timeseries analytics for the last 7 days for a link
+const response = await dub.analytics.retrieve({
+ externalId: "ext_12345",
+ interval: "7d",
+ groupBy: "timeseries",
+});
+
+const timeseries = response as ClicksTimeseries[];
+```
+
## 7. Examples
<CardGroup cols={2}>
diff --git a/apps/docs/quickstart/nextjs.mdx b/apps/docs/quickstart/nextjs.mdx
index 80150104c9..3e33f08bfd 100644
--- a/apps/docs/quickstart/nextjs.mdx
+++ b/apps/docs/quickstart/nextjs.mdx
@@ -128,24 +128,14 @@ export async function GET() {
Simliarly, you can retrieve analytics for a link using the `externalId` field.
```typescript app/api/analytics/route.ts
-import { ClicksTimeseries } from "dub/models/components";
-
-export async function GET() {
- try {
- // Retrieve the timeseries analytics for the last 7 days for a link
- const response = await dub.analytics.retrieve({
- externalId: "ext_12345",
- interval: "7d",
- groupBy: "timeseries",
- });
-
- const timeseries = response as ClicksTimeseries[];
-
- return Response.json(timeseries);
- } catch (error) {
- return Response.json({ error }, { status: 500 });
- }
-}
+// Retrieve the timeseries analytics for the last 7 days for a link
+const response = await dub.analytics.retrieve({
+ externalId: "ext_12345",
+ interval: "7d",
+ groupBy: "timeseries",
+});
+
+const timeseries = response as ClicksTimeseries[];
```
## 7. Examples
diff --git a/apps/docs/quickstart/nuxt.mdx b/apps/docs/quickstart/nuxt.mdx
index daa84a4173..183e946282 100644
--- a/apps/docs/quickstart/nuxt.mdx
+++ b/apps/docs/quickstart/nuxt.mdx
@@ -10,20 +10,30 @@ import InstallNode from "/snippets/sdk-install-nodejs.mdx";
<InstallNode />
-Create a dub instance using the Dub Node.js SDK.
+## 3. Create link
+
+Let's create a short link using the Dub Node.js SDK.
-```typescript server/utils/dub.ts
-import { Dub } from "dub";
+```typescript server/api/links.post.ts
+import { dub } from "../utils/dub";
-export const dub = new Dub({
- token: process.env.DUB_API_KEY,
- workspaceId: process.env.DUB_WORKSPACE_ID,
+export default defineEventHandler(async () => {
+ try {
+ const result = await dub.links.create({
+ url: "https://www.google.com",
+ });
+
+ return result;
+ } catch (error) {
+ console.error(error);
+ return error;
+ }
});
```
-## 3. Create link
+## 3a. Create link with `externalId`
-Let's create a short link using the Dub Node.js SDK.
+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.
```typescript server/api/links.post.ts
import { dub } from "../utils/dub";
@@ -32,6 +42,7 @@ export default defineEventHandler(async () => {
try {
const result = await dub.links.create({
url: "https://www.google.com",
+ externalId: "12345",
});
return result;
@@ -53,7 +64,6 @@ export default defineEventHandler(async () => {
try {
const result = await dub.links.upsert({
url: "https://www.google.com",
- externalId: "my-link-id",
});
return result;
@@ -68,16 +78,27 @@ export default defineEventHandler(async () => {
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_`).
+
```typescript server/api/links.patch.ts
import { dub } from "../utils/dub";
export default defineEventHandler(async () => {
try {
- const result = await dub.links.update("LINK_ID", {
- url: "https://www.google.us",
+ // Update a link by its linkId
+ const { shortLink } = await dub.links.update("clv3o9p9q000au1h0mc7r6l63", {
+ url: "https://www.google.uk", // new URL
});
- return result;
+ // Update a link by its externalId
+ const { shortLink } = await dub.links.update("ext_12345", {
+ url: "https://www.google.uk", // new URL
+ });
+
+ return { shortLink };
} catch (error) {
console.error(error);
return error;
@@ -97,9 +118,8 @@ export default defineEventHandler(async () => {
try {
// Retrieve the timeseries analytics for the last 7 days for a link
const response = await dub.analytics.retrieve({
- linkId: "LINK_ID",
+ linkId: "clv3o9p9q000au1h0mc7r6l63",
interval: "7d",
- event: "clicks",
groupBy: "timeseries",
});
@@ -113,6 +133,19 @@ export default defineEventHandler(async () => {
});
```
+Simliarly, you can retrieve analytics for a link using the `externalId` field.
+
+```typescript server/api/analytics.get.ts
+// Retrieve the timeseries analytics for the last 7 days for a link
+const response = await dub.analytics.retrieve({
+ externalId: "ext_12345",
+ interval: "7d",
+ groupBy: "timeseries",
+});
+
+const timeseries = response as ClicksTimeseries[];
+```
+
## 7. Examples
<CardGroup cols={2}>
diff --git a/apps/docs/quickstart/python.mdx b/apps/docs/quickstart/python.mdx
index 4c035bf0d8..1e1a1bbd2b 100644
--- a/apps/docs/quickstart/python.mdx
+++ b/apps/docs/quickstart/python.mdx
@@ -15,22 +15,118 @@ To install the Dub Python SDK, run the following command:
pip install dub
```
+Initialise the Dub Python SDK by creating a new instance of the `Dub` class.
+
+```python
+import os
+import dub
+from dub.models import operations
+
+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 index.py
+result = d.links.create(request=operations.CreateLinkRequestBody(
+ url='https://google.com',
+))
+
+print(result.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 index.py
+result = d.links.create(request=operations.CreateLinkRequestBody(
+ url='https://google.com',
+ external_id='12345',
+))
+
+print(result.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 index.py
+result = d.links.upsert(request=operations.UpsertLinkRequestBody(
+ url='https://google.com',
+))
+
+print(result.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 index.py
+# Update a link by its linkId
+result = d.links.update(link_id="clx1gvi9o0005hf5momm6f7hj", request_body=operations.UpdateLinkRequestBody(
+ url='https://google.uk',
+))
+
+# Update a link by its externalId
+result = d.links.update(external_id="ext_12345", request_body=operations.UpdateLinkRequestBody(
+ url='https://google.uk',
+))
+
+print(result.link_schema.short_link)
+```
+
## 6. Retrieve analytics for link
+Dub allows you to retrieve analytics for a link using the Dub Python SDK.
+
+```python index.py
+result = d.analytics.retrieve(request=operations.RetrieveAnalyticsRequest(
+ link_id="clx1gvi9o0005hf5momm6f7hj",
+ interval="7d",
+ group_by="timeseries",
+))
+
+print(result)
+```
+
+Simliarly, you can retrieve analytics for a link using the `externalId` field.
+
+```python index.py
+result = d.analytics.retrieve(request=operations.RetrieveAnalyticsRequest(
+ external_id: 'ext_12345',
+ interval="7d",
+ group_by="timeseries",
+))
+
+print(result)
+```
+
## 7. Examples
<CardGroup cols={2}>
<Card
title="Python Example"
icon="arrow-up-right-from-square"
- href="https://github.com/dubinc/examples"
+ href="https://github.com/dubinc/examples/tree/main/python/flask"
>
See the full example on GitHub.
</Card>
</CardGroup>
+```
diff --git a/apps/docs/quickstart/remix.mdx b/apps/docs/quickstart/remix.mdx
index bd9a793066..15114fe0c4 100644
--- a/apps/docs/quickstart/remix.mdx
+++ b/apps/docs/quickstart/remix.mdx
@@ -10,20 +10,35 @@ import InstallNode from "/snippets/sdk-install-nodejs.mdx";
<InstallNode />
-Create a dub instance using the Dub Node.js SDK.
+## 3. Create link
-```typescript dub.ts
-import { Dub } from "dub";
+Let's create a short link using the Dub Node.js SDK.
-export const dub = new Dub({
- token: process.env.DUB_API_KEY,
- workspaceId: process.env.DUB_WORKSPACE_ID,
-});
+```typescript app/routes/create-link.ts
+import { json } from "@remix-run/node";
+import { dub } from "~/dub";
+
+export const loader = async () => {
+ try {
+ const result = await dub.links.create({
+ url: "https://www.google.com",
+ });
+
+ return json(result, 200);
+ } catch (error: any) {
+ console.error(error);
+ return json(error, 400);
+ }
+};
```
-## 3. Create link
+<Accordion title="Full list of available attributes for the Link model">
+ <LinkArguments />
+</Accordion>
-Let's create a short link using the Dub Node.js SDK.
+## 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.
```typescript app/routes/create-link.ts
import { json } from "@remix-run/node";
@@ -33,6 +48,7 @@ export const loader = async () => {
try {
const result = await dub.links.create({
url: "https://www.google.com",
+ externalId: "12345",
});
return json(result, 200);
@@ -43,6 +59,8 @@ export const loader = async () => {
};
```
+This will let you easily [update the link](#5-update-link) later on using the `externalId`, or [retrieve analytics](#6-retrieve-analytics-for-link) for it.
+
## 4. Upsert link
Dub Node.js SDK provides a method to create or update a link so you don't have to worry about checking if the link already exists.
@@ -55,7 +73,6 @@ export const loader = async () => {
try {
const result = await dub.links.upsert({
url: "https://www.google.com",
- externalId: "my-link-id",
});
return json(result, 200);
@@ -70,17 +87,28 @@ export const loader = async () => {
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_`).
+
```typescript app/routes/update-link.ts
import { json } from "@remix-run/node";
import { dub } from "~/dub";
export const loader = async () => {
try {
- const result = await dub.links.update("LINK_ID", {
- url: "https://www.google.us",
+ // Update a link by its linkId
+ const { shortLink } = await dub.links.update("clv3o9p9q000au1h0mc7r6l63", {
+ url: "https://www.google.uk", // new URL
});
- return json(result, 200);
+ // Update a link by its externalId
+ const { shortLink } = await dub.links.update("ext_12345", {
+ url: "https://www.google.uk", // new URL
+ });
+
+ return json({ shortLink }, 200);
} catch (error: any) {
console.error(error);
return json(error, 400);
@@ -100,13 +128,14 @@ export const loader = async () => {
try {
// Retrieve the timeseries analytics for the last 7 days for a link
const result = await dub.analytics.retrieve({
- linkId: "LINK_ID",
+ linkId: "clv3o9p9q000au1h0mc7r6l63",
interval: "7d",
- event: "clicks",
groupBy: "timeseries",
});
- return json(result, 200);
+ const timeseries = response as ClicksTimeseries[];
+
+ return json(timeseries, 200);
} catch (error: any) {
console.error(error);
return json(error, 400);
@@ -114,6 +143,19 @@ export const loader = async () => {
};
```
+Simliarly, you can retrieve analytics for a link using the `externalId` field.
+
+```typescript app/routes/analytics.ts
+// Retrieve the timeseries analytics for the last 7 days for a link
+const response = await dub.analytics.retrieve({
+ externalId: "ext_12345",
+ interval: "7d",
+ groupBy: "timeseries",
+});
+
+const timeseries = response as ClicksTimeseries[];
+```
+
## 7. Examples
<CardGroup cols={2}>