Skip to main content

Get Your API Key

Start by generating your API key from your workspace settings.
1

Access Workspace Settings

  1. Log into your SundayPyjamas workspace
  2. Navigate to SettingsAPI tab
  3. Click “Generate API Key”
  4. Give your key a descriptive name (optional)
2

Save Your Key Securely

spj_ai_a1b2c3d4e5f6789012345678901234567890abcdef123456789012345678901234
Copy and store this key immediately - it won’t be shown again!

Make Your First Request

Choose your preferred method to make your first API call:
const response = await fetch('https://suite.sundaypyjamas.com/api/v1/chat', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer spj_ai_your_api_key_here',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    messages: [
      {
        role: 'user',
        content: 'Hello! Write me a professional email greeting.'
      }
    ]
  })
});

const reader = response.body.getReader();
const decoder = new TextDecoder();
let result = '';

while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  result += decoder.decode(value);
}

console.log(result);

Understanding the Response

The API returns a streaming text response. You’ll receive the AI’s response in real-time:
Hello! Here's a professional email greeting:

Dear [Recipient's Name],

I hope this email finds you well. I wanted to reach out to...

Common Use Cases

Best Practices

Secure Your API Key

# Use environment variables
export SUNDAYPYJAMAS_API_KEY="spj_ai_your_key_here"

Handle Errors Gracefully

try {
  const response = await fetch('/api/v1/chat', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${apiKey}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ messages })
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error);
  }

  // Handle streaming response...
} catch (error) {
  console.error('API Error:', error.message);
}

Optimize for Token Usage

Be concise and clear in your prompts to minimize token usage and costs.
// ❌ Too verbose
const prompt = "I would like you to please help me write a very professional business email that I need to send to my client regarding the project status update...";

// ✅ Concise and clear  
const prompt = "Write a professional email to a client with a project status update.";

Next Steps

Troubleshooting

  • Check your API key format: spj_ai_[64-characters]
  • Ensure the key is active and not deleted
  • Verify the Authorization header: Bearer spj_ai_...
  • Check your workspace usage in settings
  • Optimize prompts to use fewer tokens
  • Consider upgrading your plan
  • Verify the API URL is correct
  • Check your internet connection
  • Ensure HTTPS is used, not HTTP
Ready to build amazing AI-powered applications? Start with the Chat API documentation or explore our code examples to see what’s possible!