> ## Documentation Index
> Fetch the complete documentation index at: https://dub.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Express

> Learn how to integrate Dub with Express.

## 1. Prerequisites

To follow this guide, you will need to:

* [Create a Dub account](https://d.to/try)
* [Create an API key](https://app.dub.co/settings/tokens)

## 2. Install and initialize the Dub TypeScript SDK

<Steps titleSize="h3">
  <Step title="Install">
    Install the [Dub TypeScript SDK](/docs/sdks/typescript) using your preferred package manager:

    <Tabs>
      <Tab title="npm">
        ```bash theme={null}
        npm install dub
        ```
      </Tab>

      <Tab title="yarn">
        ```bash theme={null}
        yarn add dub zod # zod is a peer dependency
        ```
      </Tab>

      <Tab title="pnpm">
        ```bash theme={null}
        pnpm add dub 
        ```
      </Tab>
    </Tabs>
  </Step>

  <Step title="Initialize">
    Then, initialize the Dub TypeScript SDK with your API key.

    ```typescript lib/dub.ts theme={null}
    import { Dub } from "dub";

    export const dub = new Dub({
      token: process.env.DUB_API_KEY, // optional, defaults to DUB_API_KEY env variable
    });
    ```

    You can now use the `dub` object to interact with the Dub API.

    ```typescript theme={null}
    import { dub } from "./lib/dub";
    ```
  </Step>
</Steps>

## 3. Create link

Let's create a short link using the [Dub TypeScript SDK](/docs/sdks/typescript).

```typescript index.ts theme={null}
app.post("/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);
  }
});
```

<Accordion title="Full list of available attributes for the Link model">
  <ParamField body="url" type="string" required>
    The destination URL of the short link.
  </ParamField>

  <ParamField body="domain" type="string">
    The domain of the short link. If not provided, the primary domain for the
    workspace will be used (or `dub.sh` if the workspace has no domains).
  </ParamField>

  <ParamField body="key" type="string">
    The short link slug. If not provided, a random 7-character slug will be
    generated.
  </ParamField>

  <ParamField body="keyLength" type="number">
    The length of the short link slug. Defaults to 7 if not provided. When used
    with `prefix`, the total length of the key will be `prefix.length +
          keyLength`.
  </ParamField>

  <ParamField body="externalId" type="string | null">
    The ID of the link in your database. If set, it can be used to identify the
    link in future API requests (must be prefixed with 'ext\_' when passed as a
    query parameter). This key is unique across your workspace.
  </ParamField>

  <ParamField body="tenantId" type="string | null">
    The ID of the tenant that created the link inside your system. If set, it can
    be used to fetch all links for a tenant.
  </ParamField>

  <ParamField body="programId" type="string | null">
    The ID of the program the short link is associated with.
  </ParamField>

  <ParamField body="partnerId" type="string | null">
    The ID of the partner the short link is associated with.
  </ParamField>

  <ParamField body="folderId" type="string | null">
    The unique ID of an existing folder to assign the short link to.
  </ParamField>

  <ParamField body="prefix" type="string">
    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.
  </ParamField>

  <ParamField body="trackConversion" type="boolean" default={false}>
    Whether to track conversions for the short link. Defaults to `false` if not
    provided.
  </ParamField>

  <ParamField body="identifier" type="string | null">
    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.
  </ParamField>

  <ParamField body="archived" type="boolean" default={false}>
    Whether the short link is archived. Defaults to `false` if not provided.
  </ParamField>

  <ParamField body="publicStats" type="boolean" default={false}>
    Deprecated: Use `dashboard` instead. Whether the short link's stats are
    publicly accessible. Defaults to `false` if not provided.
  </ParamField>

  <ParamField body="tagIds" type="string | string[]">
    The unique IDs of the tags assigned to the short link.
  </ParamField>

  <ParamField body="tagNames" type="string | string[]">
    The unique name of the tags assigned to the short link (case insensitive).
  </ParamField>

  <ParamField body="comments" type="string | null">
    The comments for the short link.
  </ParamField>

  <ParamField body="expiresAt" type="string | null">
    The date and time when the short link will expire at.
  </ParamField>

  <ParamField body="expiredUrl" type="string | null">
    The URL to redirect to when the short link has expired.
  </ParamField>

  <ParamField body="password" type="string | null">
    The password required to access the destination URL of the short link.
  </ParamField>

  <ParamField body="proxy" type="boolean" default={false}>
    Whether the short link uses Custom Link Previews feature. Defaults to `false`
    if not provided.
  </ParamField>

  <ParamField body="title" type="string | null">
    The custom link preview title (og:title). Will be used for Custom Link
    Previews if `proxy` is true. Learn more: [https://d.to/og](https://d.to/og)
  </ParamField>

  <ParamField body="description" type="string | null">
    The custom link preview description (og:description). Will be used for Custom
    Link Previews if `proxy` is true. Learn more: [https://d.to/og](https://d.to/og)
  </ParamField>

  <ParamField body="image" type="string | null">
    The custom link preview image (og:image). Will be used for Custom Link
    Previews if `proxy` is true. Learn more: [https://d.to/og](https://d.to/og)
  </ParamField>

  <ParamField body="video" type="string | null">
    The custom link preview video (og:video). Will be used for Custom Link
    Previews if `proxy` is true. Learn more: [https://d.to/og](https://d.to/og)
  </ParamField>

  <ParamField body="rewrite" type="boolean" default={false}>
    Whether the short link uses link cloaking. Defaults to `false` if not
    provided.
  </ParamField>

  <ParamField body="ios" type="string | null">
    The iOS destination URL for the short link for iOS device targeting.
  </ParamField>

  <ParamField body="android" type="string | null">
    The Android destination URL for the short link for Android device targeting.
  </ParamField>

  <ParamField body="geo" type="object">
    Geographic targeting for the short link. Allows you to redirect users based on
    their location.
  </ParamField>

  <ParamField body="doIndex" type="boolean" default={false}>
    Allow search engines to index your short link. Defaults to `false` if not
    provided. Learn more: [https://d.to/noindex](https://d.to/noindex)
  </ParamField>

  <ParamField body="utm_source" type="string | null">
    The UTM source of the short link. If set, this will populate or override the
    UTM source in the destination URL.
  </ParamField>

  <ParamField body="utm_medium" type="string | null">
    The UTM medium of the short link. If set, this will populate or override the
    UTM medium in the destination URL.
  </ParamField>

  <ParamField body="utm_campaign" type="string | null">
    The UTM campaign of the short link. If set, this will populate or override the
    UTM campaign in the destination URL.
  </ParamField>

  <ParamField body="utm_term" type="string | null">
    The UTM term of the short link. If set, this will populate or override the UTM
    term in the destination URL.
  </ParamField>

  <ParamField body="utm_content" type="string | null">
    The UTM content of the short link. If set, this will populate or override the
    UTM content in the destination URL.
  </ParamField>

  <ParamField body="ref" type="string | null">
    The referral tag of the short link. If set, this will populate or override the
    `ref` query parameter in the destination URL.
  </ParamField>

  <ParamField body="webhookIds" type="string[] | null">
    An array of webhook IDs to trigger when the link is clicked. These webhooks
    will receive click event data.
  </ParamField>

  <ParamField body="testVariants" type="object[]">
    An array of A/B test URLs and the percentage of traffic to send to each URL.
    Requires 2-4 variants with percentages between 10-90.
  </ParamField>

  <ParamField body="testStartedAt" type="string | null">
    The date and time when the tests started.
  </ParamField>

  <ParamField body="testCompletedAt" type="string | null">
    The date and time when the tests were or will be completed.
  </ParamField>

  <ParamField body="tagId" type="string | null">
    Deprecated: Use `tagIds` instead. The unique ID of the tag assigned to the
    short link.
  </ParamField>
</Accordion>

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 theme={null}
app.post("/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);
  } catch (error: any) {
    res.status(400).json(error);
  }
});
```

This will let you easily [update the link](#5-update-link) or [retrieve analytics](#6-retrieve-analytics-for-link) for it later on using the `externalId` instead of the Dub `linkId`.

## 4. Upsert link

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.

```typescript index.ts theme={null}
app.post("/upsert-link", async (req: Request, res: Response) => {
  try {
    // Update the link if same URL already exists or create a new link
    const 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.

## 5. Update link

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_`).

```typescript index.ts theme={null}
app.post("/update-link", async (req: Request, res: Response) => {
  try {
    // Update a link by its linkId
    const { shortLink } = await dub.links.update(
      "link_rWOKByP0bRMrstK8e4HPjprJ",
      {
        url: "https://www.google.uk", // new URL
      }
    );

    // 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);
  }
});
```

## 6. Retrieve analytics for link

Dub allows you to retrieve analytics for a link using the Dub TypeScript SDK.

```typescript index.ts theme={null}
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 link
    const 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.

```typescript index.ts theme={null}
// 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}>
  <Card title="Express Example" icon="github" href="https://github.com/dubinc/examples/tree/main/typescript/express" color="#333333">
    See the full example on GitHub.
  </Card>
</CardGroup>
