The prefix of the short link slug for randomly-generated keys (e.g. if prefix
is /c/, generated keys will be in the /c/:key format). Will be ignored if
key is provided.
The identifier of the short link that is unique across your workspace. If set,
it can be used to identify your short link for client-side click tracking.
An array of webhook IDs to trigger when the link is clicked. These webhooks will receive click event data.
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.
Dub TypeScript SDK provides a method to upsert a link – where an existing link is updated if it exists, or a new link is created if it doesn’t. so you don’t have to worry about checking if the link already exists.
index.ts
app.post("/upsert-link",async(req: Request, res: Response)=>{try{// Update the link if same URL already exists or create a new linkconst result =await dub.links.upsert({ url:"https://www.google.com",}); res.status(200).json(result);}catch(error:any){ res.status(400).json(error);}});
This way, you won’t have to worry about checking if the link already exists when you’re creating it.
Let’s update an existing link using the Dub TypeScript 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_).
index.ts
app.post("/update-link",async(req: Request, res: Response)=>{try{// Update a link by its linkIdconst{ shortLink }=await dub.links.update("link_rWOKByP0bRMrstK8e4HPjprJ",{ url:"https://www.google.uk",// new URL});// Update a link by its externalIdconst{ 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);}});
Dub allows you to retrieve analytics for a link using the Dub TypeScript SDK.
index.ts
import{ ClicksTimeseries }from"dub/models/components";app.get("/analytics",async(req: Request, res: Response)=>{try{// Retrieve the timeseries analytics for the last 7 days for a linkconst response =await dub.analytics.retrieve({ linkId:"clv3o9p9q000au1h0mc7r6l63", interval:"7d", groupBy:"timeseries",});const timeseries = response as ClicksTimeseries[]; res.status(200).json(timeseries);}catch(error:any){ res.status(400).json(error);}});
Similarly, you can retrieve analytics for a link using the externalId field.
index.ts
// Retrieve the timeseries analytics for the last 7 days for a linkconst response =await dub.analytics.retrieve({ externalId:"ext_12345", interval:"7d", groupBy:"timeseries",});const timeseries = response as ClicksTimeseries[];