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]Example response:
{
"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
