Creating Links with the API
Complete guide to creating and managing short links using the Smartlinks API
Creating Links with the API
Learn how to create and manage short links programmatically using the Smartlinks API.
Basic Link Creation
The simplest way to create a link is to provide a destination URL:
JavaScript
async function createLink(destinationUrl) {
const response = await fetch('https://api.smartlinks.io/v1/links', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SMARTLINKS_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
destination_url: destinationUrl
})
});
const link = await response.json();
console.log('Created link:', link.short_url);
return link;
}
// Usage
createLink('https://example.com/my-long-url')
.then(link => console.log('Short URL:', link.short_url));
Python
import requests
import os
def create_link(destination_url):
url = 'https://api.smartlinks.io/v1/links'
headers = {
'Authorization': f'Bearer {os.getenv("SMARTLINKS_API_KEY")}',
'Content-Type': 'application/json'
}
payload = {
'destination_url': destination_url
}
response = requests.post(url, json=payload, headers=headers)
link = response.json()
print(f'Created link: {link["short_url"]}')
return link
# Usage
link = create_link('https://example.com/my-long-url')
print(f'Short URL: {link["short_url"]}')
Custom Short Links
Create branded short links with custom slugs:
async function createCustomLink(destinationUrl, customSlug) {
const response = await fetch('https://api.smartlinks.io/v1/links', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SMARTLINKS_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
destination_url: destinationUrl,
custom_slug: customSlug,
title: 'My Custom Link',
tags: ['marketing', 'campaign-2024']
})
});
return await response.json();
}
// Usage
createCustomLink('https://example.com/product', 'summer-sale');
// Creates: https://smrt.lnk/summer-sale
Link with Expiration
Create temporary links that expire after a certain time:
async function createExpiringLink(destinationUrl, expiresInDays = 7) {
const expiresAt = new Date();
expiresAt.setDate(expiresAt.getDate() + expiresInDays);
const response = await fetch('https://api.smartlinks.io/v1/links', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SMARTLINKS_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
destination_url: destinationUrl,
expires_at: expiresAt.toISOString(),
title: 'Limited Time Offer'
})
});
return await response.json();
}
Bulk Link Creation
Create multiple links efficiently:
async function createBulkLinks(urls) {
const promises = urls.map(url =>
fetch('https://api.smartlinks.io/v1/links', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SMARTLINKS_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ destination_url: url })
}).then(res => res.json())
);
const links = await Promise.all(promises);
return links;
}
// Usage
const urls = [
'https://example.com/page1',
'https://example.com/page2',
'https://example.com/page3'
];
createBulkLinks(urls)
.then(links => console.log('Created', links.length, 'links'));
Updating Links
Modify existing links:
async function updateLink(linkId, updates) {
const response = await fetch(`https://api.smartlinks.io/v1/links/${linkId}`, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${SMARTLINKS_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(updates)
});
return await response.json();
}
// Update destination URL
updateLink('link_123', {
destination_url: 'https://example.com/new-destination',
title: 'Updated Title'
});
Error Handling
Always implement proper error handling:
async function createLinkWithErrorHandling(destinationUrl) {
try {
const response = await fetch('https://api.smartlinks.io/v1/links', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SMARTLINKS_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ destination_url: destinationUrl })
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'Failed to create link');
}
return await response.json();
} catch (error) {
console.error('Error creating link:', error.message);
throw error;
}
}