ShortyyDocs

Node.js Native Fetch Integration

Use native global fetch() in Node.js 18+ to interact with Shortyy without any external packages.

Node.js Native Fetch Integration

If you prefer not to install the SDK, you can call Shortyy using Node 18+ native fetch().


Native fetch() Example

javascript
const API_KEY = process.env.SHORTYY_API_KEY;

async function createShortLink(url, expiresIn = '2h') {
  const res = await fetch('https://api.shortyy.com/v1/links', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ url, expires_in: expiresIn }),
  });

  if (res.status === 429) {
    const errorData = await res.json();
    throw new Error(`Rate limited. Try again in ${errorData.error.retryAfterSeconds}s`);
  }

  if (!res.ok) {
    const errorData = await res.json();
    throw new Error(errorData.error.message || 'Failed to create link');
  }

  const data = await res.json();
  return data.link;
}