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.

update nextjs example

Kiran Kaddc0abe15db
diff --git a/apps/docs/quickstart/nextjs.mdx b/apps/docs/quickstart/nextjs.mdx
index 242548ff09..80150104c9 100644
--- a/apps/docs/quickstart/nextjs.mdx
+++ b/apps/docs/quickstart/nextjs.mdx
@@ -15,17 +15,33 @@ import InstallNode from "/snippets/sdk-install-nodejs.mdx";
Let's create a short link using the Dub Node.js SDK.
```typescript app/api/links/route.ts
-import { Dub } from "dub";
+export async function POST() {
+ try {
+ const link = await dub.links.create({
+ url: "https://google.com",
+ });
+
+ return Response.json(link);
+ } catch (error) {
+ return Response.json({ error }, { status: 500 });
+ }
+}
+```
+
+<Accordion title="Full list of available attributes for the Link model">
+ <LinkArguments />
+</Accordion>
+
+## 3a. Create link with `externalId`
-const dub = new Dub({
- token: process.env.DUB_API_KEY,
- workspaceId: "ws_clugls3...",
-});
+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/api/links/route.ts
export async function POST() {
try {
const link = await dub.links.create({
url: "https://google.com",
+ externalId: "12345",
});
return Response.json(link);
@@ -35,6 +51,8 @@ export async function POST() {
}
```
+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.
@@ -42,11 +60,11 @@ Dub Node.js SDK provides a method to create or update a link so you don't have t
```typescript app/api/links/route.ts
export async function PATCH() {
try {
- const link = await dub.links.upsert({
+ const { shortLink } = await dub.links.upsert({
url: "https://google.com",
});
- return Response.json(link);
+ return Response.json({ shortLink }); // will always be the same short link
} catch (error) {
return Response.json({ error }, { status: 500 });
}
@@ -57,15 +75,25 @@ export async function PATCH() {
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/api/links/route.ts
export async function PATCH() {
try {
- const link = await dub.links.update("LINK_ID", {
- url: "https://www.google.uk",
- archived: true,
+ // Update a link by its linkId
+ const { shortLink } = await dub.links.update("clv3o9p9q000au1h0mc7r6l63", {
+ url: "https://www.google.uk", // new URL
});
- return Response.json(link);
+ // Update a link by its externalId
+ const { shortLink } = await dub.links.update("ext_12345", {
+ url: "https://www.google.uk", // new URL
+ });
+
+ return Response.json({ shortLink });
} catch (error) {
return Response.json({ error }, { status: 500 });
}
@@ -83,9 +111,31 @@ export async function GET() {
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",
+ groupBy: "timeseries",
+ });
+
+ const timeseries = response as ClicksTimeseries[];
+
+ return Response.json(timeseries);
+ } catch (error) {
+ return Response.json({ error }, { status: 500 });
+ }
+}
+```
+
+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",
- event: "clicks",
groupBy: "timeseries",
});
diff --git a/apps/docs/quickstart/nodejs.mdx b/apps/docs/quickstart/nodejs.mdx
index 96cbb5342f..0801a1f455 100644
--- a/apps/docs/quickstart/nodejs.mdx
+++ b/apps/docs/quickstart/nodejs.mdx
@@ -96,7 +96,6 @@ Simliarly, you can retrieve analytics for a link using the `externalId` field.
import { ClicksTimeseries } from "dub/models/components";
// Retrieve the timeseries analytics for the last 7 days for a link
-
const response = await dub.analytics.retrieve({
externalId: "ext_12345",
interval: "7d",