@tinyurl-ca/sdk
v1.0.1
Published
Official JavaScript/TypeScript SDK for TinyURL.ca API
Maintainers
Readme
TinyURL.ca JavaScript/TypeScript SDK
Official JavaScript/TypeScript SDK for the TinyURL.ca API.
Installation
npm install @tinyurl-ca/sdkQuick Start
import { TinyURLClient } from '@tinyurl-ca/sdk';
// Initialize client
const client = new TinyURLClient({
apiKey: 'your_api_key_here'
});
// Shorten a URL
const link = await client.createLink({
url: 'https://example.com'
});
console.log(link.shortUrl); // https://tinyurl.ca/abc123API Reference
Initialize Client
import { TinyURLClient } from '@tinyurl-ca/sdk';
const client = new TinyURLClient({
apiKey: 'your_api_key_here',
baseURL: 'https://tinyurl.ca/api/v1' // optional
});Create a Link
const link = await client.createLink({
url: 'https://example.com',
customCode: 'my-link', // optional
title: 'My Website', // optional
expiresIn: 86400 // optional, seconds
});
// Response
{
id: 'abc123',
shortCode: 'my-link',
shortUrl: 'https://tinyurl.ca/my-link',
originalUrl: 'https://example.com',
title: 'My Website',
clicks: 0,
createdAt: '2026-04-04T00:00:00Z',
expiresAt: '2026-04-05T00:00:00Z'
}Get Link Details
const link = await client.getLink('link_id');List Links
const links = await client.listLinks({
limit: 50, // optional, default 50
offset: 0 // optional, default 0
});Delete Link
await client.deleteLink('link_id');Shorten URL (convenience method)
const shortUrl = await client.shorten('https://example.com');
// Returns: 'https://tinyurl.ca/abc123'
// With custom code
const customUrl = await client.shorten('https://example.com', 'promo');
// Returns: 'https://tinyurl.ca/promo'TypeScript Support
Full TypeScript support with type definitions included.
import { TinyURLClient, Link, CreateLinkOptions } from '@tinyurl-ca/sdk';
const options: CreateLinkOptions = {
url: 'https://example.com',
customCode: 'my-link'
};
const link: Link = await client.createLink(options);Error Handling
try {
const link = await client.createLink({
url: 'https://example.com',
customCode: 'taken'
});
} catch (error) {
if (error.response?.status === 409) {
console.log('Custom code already in use');
} else {
console.error('Error:', error.message);
}
}Examples
Node.js
const { TinyURLClient } = require('@tinyurl-ca/sdk');
const client = new TinyURLClient({
apiKey: process.env.TINYURL_API_KEY
});
async function main() {
// Create link
const link = await client.createLink({
url: 'https://mywebsite.com',
title: 'My Website'
});
console.log('Short URL:', link.shortUrl);
// List all links
const links = await client.listLinks();
console.log('Total links:', links.length);
}
main();Next.js API Route
import { TinyURLClient } from '@tinyurl-ca/sdk';
import { NextRequest, NextResponse } from 'next/server';
const client = new TinyURLClient({
apiKey: process.env.TINYURL_API_KEY!
});
export async function POST(request: NextRequest) {
const { url } = await request.json();
const link = await client.createLink({ url });
return NextResponse.json({ shortUrl: link.shortUrl });
}Express.js
const express = require('express');
const { TinyURLClient } = require('@tinyurl-ca/sdk');
const app = express();
const client = new TinyURLClient({
apiKey: process.env.TINYURL_API_KEY
});
app.post('/shorten', async (req, res) => {
try {
const { url } = req.body;
const link = await client.createLink({ url });
res.json({ shortUrl: link.shortUrl });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000);License
MIT
Support
- Documentation: https://tinyurl.ca/docs
- Issues: https://github.com/raj-iwt/tinyurl-sdk/issues
- Email: [email protected]
