Bitly Node.js API Wrapper

0.0.6 · abandoned · verified Tue Apr 21

bitly-node-api is a wrapper for the Bitly v4 REST API, supporting various JavaScript execution environments and paradigms. It offers compatibility with ECMAScript 5, 6, 8, TypeScript, and integrates with modern async/await and Promises, alongside traditional callbacks. The library is designed to return pure JSON responses and is noted for its compatibility with serverless functions like AWS Lambda. Despite its flexible design, the package is currently at version 0.0.6 and has not seen updates since March 2019, indicating it is no longer actively maintained. Developers should be aware that it might not support the latest Bitly API features or security standards. Key differentiators include its broad JavaScript version support and promise/callback flexibility, though its age is a significant consideration.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `bitly-node-api` client with an access token (preferably from environment variables), then use it to create a new short Bitlink, expand it to retrieve its details, and finally fetch its click count.

import * as BitlyAPI from 'bitly-node-api';

const BITLY_USER_TOKEN = process.env.BITLY_USER_TOKEN ?? 'YOUR_BITLY_ACCESS_TOKEN_HERE';

// Ensure a token is available
if (!BITLY_USER_TOKEN || BITLY_USER_TOKEN === 'YOUR_BITLY_ACCESS_TOKEN_HERE') {
  console.error('ERROR: Please set BITLY_USER_TOKEN environment variable or replace placeholder.');
  process.exit(1);
}

const bitly = new BitlyAPI();
bitly.setUserToken(BITLY_USER_TOKEN);

async function createAndExpandBitlink() {
  try {
    const longUrl = 'https://www.example.com/a-very-long-url-for-testing-bitly-api-wrapper-' + Date.now();
    const payload = {
      long_url: longUrl,
      domain: 'bit.ly' // Specify a custom domain if you have one, otherwise omit.
    };
    console.log('Attempting to create a Bitlink for:', longUrl);

    // Create a new Bitlink
    const createResponse = await bitly.bitlinks.create(payload);
    console.log('Successfully created Bitlink:', createResponse.link);
    console.log('Original URL:', createResponse.long_url);

    // Expand the created Bitlink to retrieve full details
    console.log('\nAttempting to expand the Bitlink...');
    const expandResponse = await bitly.bitlinks.expand(createResponse.link);
    console.log('Expanded Bitlink details:', expandResponse);

    // Example: Get clicks for the Bitlink (assuming the API supports this and token has scope)
    console.log('\nAttempting to get click count for the Bitlink...');
    const clicksResponse = await bitly.bitlinks.getClicks(createResponse.link);
    console.log('Total clicks:', clicksResponse.total_clicks);

  } catch (error: any) {
    console.error('An error occurred during Bitlink operations:', error.message || error);
    if (error.response && error.response.data) {
      console.error('Bitly API Error Details:', error.response.data);
    }
  }
}

createAndExpandBitlink();

view raw JSON →