The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
allow random generation if key is blank
Steven Tey14f28bf8cf9f
diff --git a/pages/api/index.ts b/pages/api/index.tsindex c1260bf576..beb89de20c 100644--- a/pages/api/index.ts+++ b/pages/api/index.ts@@ -164,23 +164,138 @@ export default async function handler(): Promise<{name: "slug",description:- "The slug for the project to retrieve links for. E.g. for app.dub.co/vercel, the slug is 'vercel'.",+ "The slug for the project to create links for. E.g. for app.dub.co/vercel, the slug is 'vercel'.",in: "query",required: true,schema: {description:- "The slug for the project to retrieve links for. E.g. for app.dub.co/vercel, the slug is 'vercel'.",+ "The slug for the project to create links for. E.g. for app.dub.co/vercel, the slug is 'vercel'.",type: "string",},},],requestBody: {- description: "The link to create.",+ description: "Details of the link to create.",required: true,content: {"application/json": {schema: {- $ref: "#/components/schemas/Link",+ type: "object",+ properties: {+ domain: {+ type: "string",+ description: "The domain of the short link.",+ required: true,+ },+ url: {+ type: "string",+ description: "The destination URL of the short link.",+ required: true,+ },+ key: {+ type: "string",+ description:+ "The short link slug. If not provided, a random 7-character slug will be generated.",+ },+ archived: {+ type: "boolean",+ description: "Whether the short link is archived.",+ default: false,+ },+ expiresAt: {+ type: "string",+ format: "date-time",+ description:+ "The date and time when the short link will expire.",+ default: null,+ nullable: true,+ },+ password: {+ type: "string",+ description:+ "The password required to access the destination URL of the short link.",+ default: null,+ nullable: true,+ },+ proxy: {+ type: "boolean",+ description:+ "Whether the short link uses Custom Social Media Cards feature.",+ default: false,+ },+ title: {+ type: "string",+ description:+ "The title of the short link generated via api.dub.co/metatags. Will be used for Custom Social Media Cards if `proxy` is true.",+ default: null,+ nullable: true,+ },+ description: {+ type: "string",+ description:+ "The description of the short link generated via api.dub.co/metatags. Will be used for Custom Social Media Cards if `proxy` is true.",+ default: null,+ nullable: true,+ },+ image: {+ type: "string",+ description:+ "The image of the short link generated via api.dub.co/metatags. Will be used for Custom Social Media Cards if `proxy` is true.",+ default: null,+ nullable: true,+ },+ rewrite: {+ type: "boolean",+ description: "Whether the short link uses link cloaking.",+ default: false,+ },+ ios: {+ type: "string",+ description:+ "The iOS destination URL for the short link for iOS device targeting.",+ default: null,+ nullable: true,+ },+ android: {+ type: "string",+ description:+ "The Android destination URL for the short link for Android device targeting.",+ default: null,+ nullable: true,+ },+ geo: {+ type: "object",+ description:+ "Geo targeting information for the short link.",+ additionalProperties: {+ type: "string",+ format: "uri",+ },+ default: null,+ nullable: true,+ },+ publicStats: {+ type: "boolean",+ description:+ "Whether the short link's stats are publicly accessible.",+ default: false,+ },+ tagId: {+ type: "string",+ format: "cuid",+ description:+ "The unique id of the tag assigned to the short link.",+ default: null,+ nullable: true,+ },+ comments: {+ type: "string",+ description: "The comments for the short link.",+ default: null,+ nullable: true,+ },+ },+ required: ["domain", "url"],},},},@@ -327,6 +442,12 @@ export default async function handler(): Promise<Link: {type: "object",properties: {+ id: {+ type: "string",+ format: "cuid",+ description: "The unique ID of the short link.",+ readOnly: true,+ },domain: {type: "string",description: "The domain of the short link.",diff --git a/pages/api/links/index.ts b/pages/api/links/index.tsindex fbe52d6701..3fcb8f419a 100644--- a/pages/api/links/index.ts+++ b/pages/api/links/index.ts@@ -1,4 +1,9 @@-import { addLink, getLinksForProject, processKey } from "#/lib/api/links";+import {+ addLink,+ getLinksForProject,+ getRandomKey,+ processKey,+} from "#/lib/api/links";import { withLinksAuth } from "#/lib/auth";import { isBlacklistedDomain, isBlacklistedKey } from "#/lib/edge-config";import { getApexDomain, log } from "#/lib/utils";@@ -42,8 +47,15 @@ export default withLinksAuth(// POST /api/links – create a new link} else if (req.method === "POST") {let { domain, key, url, rewrite, geo } = req.body;- if (!domain || !key || !url) {- return res.status(400).end("Missing domain or key or url.");+ if (!url) {+ return res.status(400).end("Missing destination url.");+ }+ if (!domain) {+ return res.status(400).end("Missing short link domain.");+ }++ if (!key) {+ key = await getRandomKey(domain);}// if it's not a custom project, do some filtering