1. Prerequisites

To follow this guide, you will need to:

2. Install and initialize the CodeQR TypeScript SDK

1

Install

Install the CodeQR TypeScript SDK using your preferred package manager:

npm install codeqr
2

Initialize

Then, initialize the CodeQR TypeScript SDK with your API key.

lib/codeqr.ts
import { CodeQR } from "codeqr";

export const codeqr = new CodeQR({
  token: process.env.CODEQR_API_KEY, // optional, defaults to CODEQR_API_KEY env variable
});

You can now use the codeqr object to interact with the CodeQR API.

import { codeqr } from "./lib/codeqr";

Let’s create a short link using the CodeQR TypeScript SDK.

app/routes/create-link.ts
export const loader = async () => {
  try {
    const result = await codeqr.links.create({
      url: "https://www.google.com",
    });

    return json(result, 200);
  } catch (error: any) {
    console.error(error);
    return json(error, 400);
  }
};

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 CodeQR’s system.

app/routes/create-link.ts
export const loader = async () => {
  try {
    const result = await codeqr.links.create({
      url: "https://www.google.com",
      externalId: "12345",
    });

    return json(result, 200);
  } catch (error: any) {
    console.error(error);
    return json(error, 400);
  }
};

This will let you easily update the link or retrieve analytics for it later on using the externalId instead of the CodeQR linkId.

CodeQR 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.

app/routes/upsert-link.ts
export const loader = async () => {
  try {
    const result = await codeqr.links.upsert({
      url: "https://www.google.com",
    });

    return json(result, 200);
  } catch (error: any) {
    console.error(error);
    return json(error, 400);
  }
};

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 CodeQR TypeScript SDK.

You can do that in two ways:

  • Using the link’s linkId in CodeQR’s system.
  • Using the link’s externalId in your own database (prefixed with ext_).
app/routes/update-link.ts
export const loader = async () => {
  try {
    // Update a link by its linkId
    const { shortLink } = await codeqr.links.update(
      "link_rWOKByP0bRMrstK8e4HPjprJ",
      {
        url: "https://www.google.uk", // new URL
      }
    );

    // Update a link by its externalId
    const { shortLink } = await codeqr.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);
  }
};

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

app/routes/analytics.ts
export const loader = async () => {
  try {
    // Retrieve the timeseries analytics for the last 7 days for a link
    const result = await codeqr.analytics.retrieve({
      linkId: "clv3o9p9q000au1h0mc7r6l63",
      interval: "7d",
      groupBy: "timeseries",
    });

    const timeseries = response as ClicksTimeseries[];

    return json(timeseries, 200);
  } catch (error: any) {
    console.error(error);
    return json(error, 400);
  }
};

Similarly, you can retrieve analytics for a link using the externalId field.

app/routes/analytics.ts
// Retrieve the timeseries analytics for the last 7 days for a link
const response = await codeqr.analytics.retrieve({
  externalId: "ext_12345",
  interval: "7d",
  groupBy: "timeseries",
});

const timeseries = response as ClicksTimeseries[];

7. Examples