Authentication

Learn how to authenticate with the HelpingAI API and manage your API keys securely.

Getting Your API Key

  1. Sign up at helpingai.co
  2. Navigate to your dashboard
  3. Click "API Keys" in the sidebar
  4. Generate a new API key
  5. Copy and store your key securely

Important: Keep your API key secure and never expose it in client-side code or public repositories.

Using Your API Key

Set your API key as an environment variable:

bash
# Linux/macOS {#linuxmacos}
export HELPINGAI_API_KEY="your-api-key-here"

# Windows {#windows}
set HELPINGAI_API_KEY=your-api-key-here

Python (using requests)

python
import os
import requests

api_key = os.getenv("HELPINGAI_API_KEY")
headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

response = requests.post(
    "https://api.helpingai.co/v1/chat/completions",
    headers=headers,
    json={
        "model": "Dhanishtha-2.0-preview",
        "messages": [{"role": "user", "content": "Hello!"}]
    }
)

Python (using OpenAI SDK)

python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.helpingai.co/v1",
    api_key=os.getenv("HELPINGAI_API_KEY")
)

response = client.chat.completions.create(
    model="Dhanishtha-2.0-preview",
    messages=[{"role": "user", "content": "Hello!"}]
)

Python (using HelpingAI SDK)

python
import os
from helpingai import HelpingAI

client = HelpingAI(api_key=os.getenv("HELPINGAI_API_KEY"))

response = client.chat.completions.create(
    model="Dhanishtha-2.0-preview",
    messages=[{"role": "user", "content": "Hello!"}]
)

JavaScript (using axios)

javascript
const axios = require('axios');

const apiKey = process.env.HELPINGAI_API_KEY;

const response = await axios.post(
  'https://api.helpingai.co/v1/chat/completions',
  {
    model: 'Dhanishtha-2.0-preview',
    messages: [{role: 'user', content: 'Hello!'}]
  },
  {
    headers: {
      'Authorization': `Bearer ${apiKey}`,
      'Content-Type': 'application/json'
    }
  }
);

JavaScript (using OpenAI package)

javascript
import OpenAI from 'openai';

const openai = new OpenAI({
  baseURL: 'https://api.helpingai.co/v1',
  apiKey: process.env.HELPINGAI_API_KEY
});

const response = await openai.chat.completions.create({
  model: 'Dhanishtha-2.0-preview',
  messages: [{role: 'user', content: 'Hello!'}]
});

JavaScript (using HelpingAI SDK)

javascript
import { HelpingAI } from 'helpingai';

const client = new HelpingAI({
  apiKey: process.env.HELPINGAI_API_KEY
});

const response = await client.chat.completions.create({
  model: 'Dhanishtha-2.0-preview',
  messages: [{role: 'user', content: 'Hello!'}]
});

Direct API Key Usage

If you can't use environment variables, you can pass the API key directly:

Python (using requests)

python
import requests

headers = {
    "Authorization": "Bearer your-api-key-here",
    "Content-Type": "application/json"
}

response = requests.post(
    "https://api.helpingai.co/v1/chat/completions",
    headers=headers,
    json={
        "model": "Dhanishtha-2.0-preview",
        "messages": [{"role": "user", "content": "Hello!"}]
    }
)

Python (using OpenAI SDK)

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.helpingai.co/v1",
    api_key="your-api-key-here"
)

Python (using HelpingAI SDK)

python
from helpingai import HelpingAI

client = HelpingAI(api_key="your-api-key-here")

JavaScript (using axios)

javascript
const response = await axios.post(
  'https://api.helpingai.co/v1/chat/completions',
  {
    model: 'Dhanishtha-2.0-preview',
    messages: [{role: 'user', content: 'Hello!'}]
  },
  {
    headers: {
      'Authorization': 'Bearer your-api-key-here',
      'Content-Type': 'application/json'
    }
  }
);

JavaScript (using OpenAI package)

javascript
const openai = new OpenAI({
  baseURL: 'https://api.helpingai.co/v1',
  apiKey: 'your-api-key-here'
});

JavaScript (using HelpingAI SDK)

javascript
const client = new HelpingAI({
  apiKey: 'your-api-key-here'
});

API Key Management

Best Practices

  1. Use Environment Variables: Never hardcode API keys in your source code
  2. Rotate Keys Regularly: Generate new keys periodically for security
  3. Use Different Keys: Use separate keys for development, staging, and production
  4. Monitor Usage: Track API key usage in your dashboard
  5. Revoke Compromised Keys: Immediately revoke any potentially compromised keys

Key Rotation

To rotate your API key:

  1. Generate a new API key in your dashboard
  2. Update your environment variables or configuration
  3. Test your application with the new key
  4. Revoke the old key once confirmed working

Multiple API Keys

You can create multiple API keys for different purposes:

  • Development: For local development and testing
  • Staging: For staging environment
  • Production: For production applications
  • CI/CD: For automated testing and deployment

Security Considerations

Never Expose API Keys

Don't do this:

javascript
// Never put API keys in client-side code
const apiKey = "sk-helpingai-abc123..."; // Visible to users!

// Never commit API keys to version control
const config = {
  apiKey: "sk-helpingai-abc123..." // Will be in git history!
};

Do this instead:

javascript
// Use environment variables
const apiKey = process.env.HELPINGAI_API_KEY;

// Or use a secure configuration service
const apiKey = await getSecretFromVault('helpingai-api-key');

Server-Side Only

API keys should only be used in server-side applications:

  • ✅ Node.js backend servers
  • ✅ Python web applications
  • ✅ Server-side scripts and cron jobs
  • ❌ Frontend JavaScript (React, Vue, etc.)
  • ❌ Mobile apps (iOS, Android)
  • ❌ Desktop applications distributed to users

Proxy Pattern for Client Applications

For client-side applications, create a proxy endpoint:

javascript
// Your backend server
app.post('/api/chat', async (req, res) => {
  const response = await fetch('https://api.helpingai.co/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.HELPINGAI_API_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(req.body)
  });
  
  const data = await response.json();
  res.json(data);
});

// Your frontend
const response = await fetch('/api/chat', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    model: 'Dhanishtha-2.0-preview',
    messages: [{role: 'user', content: 'Hello!'}]
  })
});

Error Handling

Authentication Errors

Handle authentication errors gracefully:

Python

python
try:
    response = client.chat.completions.create(
        model="Dhanishtha-2.0-preview",
        messages=[{"role": "user", "content": "Hello!"}]
    )
except Exception as e:
    if "authentication" in str(e).lower():
        print("Invalid API key. Please check your credentials.")
    elif "unauthorized" in str(e).lower():
        print("API key may be expired or revoked.")
    else:
        print(f"Error: {e}")

JavaScript

javascript
try {
  const response = await client.chat.completions.create({
    model: 'Dhanishtha-2.0-preview',
    messages: [{role: 'user', content: 'Hello!'}]
  });
} catch (error) {
  if (error.status === 401) {
    console.error('Invalid API key. Please check your credentials.');
  } else if (error.status === 403) {
    console.error('API key may be expired or insufficient permissions.');
  } else {
    console.error('Error:', error.message);
  }
}

Testing Authentication

Test your authentication setup:

Python

python
def test_authentication():
    try:
        response = client.chat.completions.create(
            model="Dhanishtha-2.0-preview",
            messages=[{"role": "user", "content": "Test"}],
            max_tokens=5
        )
        print("✅ Authentication successful!")
        return True
    except Exception as e:
        print(f"❌ Authentication failed: {e}")
        return False

test_authentication()

JavaScript

javascript
async function testAuthentication() {
  try {
    const response = await client.chat.completions.create({
      model: 'Dhanishtha-2.0-preview',
      messages: [{role: 'user', content: 'Test'}],
      max_tokens: 5
    });
    console.log('✅ Authentication successful!');
    return true;
  } catch (error) {
    console.error('❌ Authentication failed:', error.message);
    return false;
  }
}

testAuthentication();

Next Steps

Once you have authentication working:

  1. Make your first API call - Start building with HelpingAI
  2. Understand models - Learn about available models
  3. Explore the API - Dive into the full API reference

Support

Having trouble with authentication?