Create a link
Creating a short link in U301 is simple. Here's a minimal example:
import { U301 } from 'u301';
const apiKey = '<YOUR_API_KEY>';
const workspaceId = '<YOUR_WORKSPACE_ID>';
const u301 = new U301({
apiKey,
workspaceId,
});
const link = await u301.links.create('https://example.com'); // [!code highlight]
console.log(link);Request
| Name | Location | Type | Required | Default | Description |
|---|---|---|---|---|---|
| Authorization | Header | string | Bearer token used for authentication. | ||
| workspaceId | Query | string | Workspace identifier for multi‑tenant routing. | ||
| url | Body | string | Destination URL to shorten. | ||
| domain | Body | string | u301.co | Domain for the short link. | |
| slug | Body | string | Random string | Custom path segment. When omitted, a random slug is generated. | |
| reuseExisting | Body | boolean | false | Reuse an existing short link if the same url already exists. | |
| password | Body | string | null | Password required to open the short link. | |
| comment | Body | string | null | Comment for short URL |
Response
| Name | Type | Description |
|---|---|---|
| id | string | Unique identifier of the link. |
| url | string | Original destination URL. |
| slug | string | Path segment of the short link. |
| isCustomSlug | boolean | Whether the slug was provided by the user. |
| domain | string | Domain used for the short link. |
| isReused | boolean | Indicates the link was reused due to reuseExisting. |
| shortLink | string | Fully qualified short link ({domain}/{slug}). |
| comment | string | Optional note attached to the link. |
{
"id": "019a7d8c-4a85-7aa4-9862-ee2008f54854",
"url": "https://example.com",
"slug": "jtyZ",
"isCustomSlug": false,
"domain": "u301.co",
"isReused": false,
"shortLink": "u301.co/jtyZ",
"comment": ""
}Bulk create links
Create multiple links in one call by passing an array to createMany:
const link = await u301.links.createMany([
'https://example.com',
'https://example.org',
]);Custom domain and path (slug)
Want a branded short link, like a fixed path https://u301.co/launch, or your own domain instead of u301.co? Provide domain and slug:
const link = await u301.links.create({
url: 'https://example.com',
domain: 'u301.co', // [!code highlight]
slug: 'launch', // [!code highlight]
});Handle duplicate URLs
If the target URL already exists, you may prefer to reuse the existing short link instead of creating a new one. Set reuseExisting to true:
const link = await u301.links.create({
url: 'https://example.com',
reuseExisting: true, // [!code highlight]
});The response marks isReused as true:
{
"id": "019a7d8c-4a85-7aa4-9862-ee2008f54854",
"url": "https://example.com",
"slug": "jtyZ",
"isCustomSlug": false,
"domain": "u301.co",
"isReused": true,
"shortLink": "u301.co/jtyZ",
"comment": ""
}Password-protect a short link
Require a password before resolving to the target URL by setting password:
const link = await u301.links.create({
url: 'https://example.com',
password: 'example-password', // [!code highlight]
});Try https://u301.co/4jnB with password example-password.
Last updated on