Authentication Guide

    Learn how to authenticate with the Smartlinks API using API keys and bearer tokens

    Authentication Guide

    Welcome to the Smartlinks API authentication guide. This tutorial will walk you through the process of authenticating your API requests.

    Overview

    The Smartlinks API uses API key authentication. All requests must include your API key in the Authorization header.

    Getting Your API Key

    1. Log into your Smartlinks dashboard
    2. Navigate to Settings > API Keys
    3. Click Generate New API Key
    4. Copy and securely store your API key

    Important: Keep your API keys secure and never commit them to version control. Use environment variables instead.

    Making Authenticated Requests

    JavaScript/Node.js

    const fetch = require('node-fetch');
    
    const SMARTLINKS_API_KEY = process.env.SMARTLINKS_API_KEY;
    const BASE_URL = 'https://api.smartlinks.io/v1';
    
    async function makeAuthenticatedRequest() {
      const response = await fetch(`${BASE_URL}/links`, {
        method: 'GET',
        headers: {
          'Authorization': `Bearer ${SMARTLINKS_API_KEY}`,
          'Content-Type': 'application/json'
        }
      });
      
      const data = await response.json();
      return data;
    }
    
    makeAuthenticatedRequest()
      .then(data => console.log(data))
      .catch(error => console.error('Error:', error));
    

    Python

    import os
    import requests
    
    SMARTLINKS_API_KEY = os.getenv('SMARTLINKS_API_KEY')
    BASE_URL = 'https://api.smartlinks.io/v1'
    
    headers = {
        'Authorization': f'Bearer {SMARTLINKS_API_KEY}',
        'Content-Type': 'application/json'
    }
    
    response = requests.get(f'{BASE_URL}/links', headers=headers)
    data = response.json()
    
    print(data)
    

    cURL

    curl -X GET "https://api.smartlinks.io/v1/links" \
      -H "Authorization: Bearer YOUR_API_KEY" \
      -H "Content-Type: application/json"
    

    Environment Variables

    Always store your API keys in environment variables:

    .env file

    SMARTLINKS_API_KEY=your_api_key_here
    

    Loading in Node.js

    require('dotenv').config();
    const apiKey = process.env.SMARTLINKS_API_KEY;
    

    Error Handling

    Handle authentication errors gracefully:

    async function authenticatedFetch(endpoint) {
      try {
        const response = await fetch(`${BASE_URL}${endpoint}`, {
          headers: {
            'Authorization': `Bearer ${SMARTLINKS_API_KEY}`
          }
        });
        
        if (response.status === 401) {
          throw new Error('Invalid API key');
        }
        
        if (response.status === 403) {
          throw new Error('Access forbidden');
        }
        
        return await response.json();
      } catch (error) {
        console.error('Authentication error:', error);
        throw error;
      }
    }
    

    Best Practices

    • ✅ Store API keys in environment variables
    • ✅ Use HTTPS for all API requests
    • ✅ Rotate API keys periodically
    • ✅ Implement rate limiting on your end
    • ❌ Never expose API keys in client-side code
    • ❌ Never commit API keys to version control

    Next Steps