Get Your API Key
Start by generating your API key from your workspace settings.
Access Workspace Settings
Log into your SundayPyjamas workspace
Navigate to Settings → API tab
Click “Generate API Key”
Give your key a descriptive name (optional)
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 );
import requests
response = requests.post(
'https://suite.sundaypyjamas.com/api/v1/chat' ,
headers = {
'Authorization' : 'Bearer spj_ai_your_api_key_here' ,
'Content-Type' : 'application/json' ,
},
json = {
'messages' : [
{
'role' : 'user' ,
'content' : 'Hello! Write me a professional email greeting.'
}
]
},
stream = True
)
full_response = ''
for chunk in response.iter_content( chunk_size = None , decode_unicode = True ):
if chunk:
full_response += chunk
print (full_response)
curl -X POST https://suite.sundaypyjamas.com/api/v1/chat \
-H "Authorization: Bearer spj_ai_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "Hello! Write me a professional email greeting."
}
]
}'
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
Content Generation Generate blog posts, emails, and marketing copy with custom prompts.
Chat Interface Build conversational AI interfaces with streaming responses.
Email Writing Create professional emails for various purposes and audiences.
Code Assistance Get help with programming tasks and code generation.
Best Practices
Secure Your API Key
Environment Variables
In Your Code
# 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
Authentication Guide Learn about API key management, security, and permissions.
Chat API Reference Explore complete endpoint documentation with all parameters.
Code Examples View ready-to-use implementations in multiple languages.
Rate Limits Understand usage guidelines and optimization strategies.
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_...
"Token limit exceeded" Error
Check your workspace usage in settings
Optimize prompts to use fewer tokens
Consider upgrading your plan
Network/Connection Errors
Verify the API URL is correct
Check your internet connection
Ensure HTTPS is used, not HTTP