Skip to main content
GET
/
credits-balance
Get Credits Balance
curl --request GET \
  --url https://api.pre.dev/credits-balance \
  --header 'Authorization: Bearer <token>'
{
  "success": true,
  "creditsRemaining": 450
}

GET /credits-balance

Get the remaining prototype credits balance for the authenticated API key holder.

Overview

  • Use Cases: Monitor credit usage, check balance before making requests
  • Processing Time: Instant
  • Response: JSON object with creditsRemaining count

Endpoint

GET https://api.pre.dev/credits-balance

Headers

Authorization: Bearer YOUR_API_KEY

Parameters

No request body required. Authentication is via the Authorization header.

Example Request

curl https://api.pre.dev/credits-balance \
  -H "Authorization: Bearer YOUR_API_KEY"

Response

Success Response

{
  "success": true,
  "creditsRemaining": 450
}
FieldTypeDescription
successbooleanWhether the request succeeded
creditsRemainingnumberNumber of prototype credits remaining for this API key

Error Response - Missing Context

{
  "error": "Missing context",
  "message": "Unable to determine user or organization from API key"
}

Error Response - Server Error

{
  "error": "Failed to retrieve credits balance",
  "message": "Internal server error details"
}

Credit Types and Priority

Credits are consumed in this priority order:
  1. Prototype Credits - Used first if available
  2. Daily Allocation - Based on subscription tier
  3. Rollover Credits - Unused daily credits (if applicable)
This endpoint returns only the prototype credits balance.

Credit Costs

EndpointCostWhen Charged
Fast Spec~5-10 creditsPer-inference during generation
Deep Spec~10-50 creditsPer-inference during generation

Code Examples

cURL

curl https://api.pre.dev/credits-balance \
  -H "Authorization: Bearer YOUR_API_KEY"

Python

import requests

def get_credits_balance(api_key: str) -> dict:
    """Get the current credits balance for the API key."""
    url = "https://api.pre.dev/credits-balance"
    headers = {"Authorization": f"Bearer {api_key}"}
    
    try:
        response = requests.get(url, headers=headers, timeout=60)
        response.raise_for_status()
        data = response.json()
        return {
            "success": data["success"],
            "creditsRemaining": data["creditsRemaining"]
        }
    except requests.RequestException as e:
        raise Exception(f"Request failed: {str(e)}") from e

# Usage
api_key = "YOUR_API_KEY"
balance = get_credits_balance(api_key)
print(f"Credits remaining: {balance['creditsRemaining']}")

JavaScript/Node.js

async function getCreditsBalance(apiKey) {
  const url = 'https://api.pre.dev/credits-balance';
  
  try {
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${apiKey}`
      }
    });
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'Failed to get credits balance');
    }
    
    return await response.json();
  } catch (error) {
    throw new Error(`Request failed: ${error.message}`);
  }
}

// Usage
try {
  const balance = await getCreditsBalance('YOUR_API_KEY');
  console.log(`Credits remaining: ${balance.creditsRemaining}`);
} catch (error) {
  console.error('Error:', error.message);
}

TypeScript

interface CreditsBalanceResponse {
  success: boolean;
  creditsRemaining: number;
}

async function getCreditsBalance(apiKey: string): Promise<CreditsBalanceResponse> {
  const url = 'https://api.pre.dev/credits-balance';
  
  try {
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${apiKey}`
      }
    });
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'Failed to get credits balance');
    }
    
    return await response.json();
  } catch (error) {
    if (error instanceof Error) {
      throw new Error(`Request failed: ${error.message}`);
    }
    throw error;
  }
}

// Usage
try {
  const balance = await getCreditsBalance('YOUR_API_KEY');
  console.log(`Credits remaining: ${balance.creditsRemaining}`);
  
  if (balance.creditsRemaining < 20) {
    console.warn('⚠️ Low credits! Consider purchasing more.');
  }
} catch (error) {
  console.error('Error:', error);
}

Use Cases

Before Making Expensive Requests

Check your balance before initiating a spec request:
const balance = await getCreditsBalance(apiKey);

if (balance.creditsRemaining > 0) {
  // Safe to make spec request
  const spec = await generateDeepSpec({
    input: "Build an enterprise platform"
  });
} else {
  console.log(`Insufficient credits. Have ${balance.creditsRemaining}`);
}

Monitor Credit Usage

Track credit consumption over time:
async function checkAndNotify(apiKey: string) {
  const balance = await getCreditsBalance(apiKey);
  
  if (balance.creditsRemaining < 50) {
    // Send low balance notification
    await sendAlert(`Only ${balance.creditsRemaining} credits remaining`);
  }
}

Dashboard Access

Solo Users

View your credits balance via the dashboard:

Enterprise Users

Access credits and billing information:

Error Handling

Authentication Failures

# Invalid API key
curl https://api.pre.dev/credits-balance \
  -H "Authorization: Bearer invalid_key"
Response:
{
  "error": "Unauthorized",
  "message": "Invalid API key"
}

Network Issues

Implement retry logic with exponential backoff:
async function getCreditsBalanceWithRetry(
  apiKey: string,
  maxRetries: number = 3
): Promise<CreditsBalanceResponse> {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      return await getCreditsBalance(apiKey);
    } catch (error) {
      if (attempt === maxRetries - 1) throw error;
      
      const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
      await new Promise(resolve => setTimeout(resolve, delay));
    }
  }
  
  throw new Error('Max retries exceeded');
}

Best Practices

Regular Monitoring

  • Check balance before making expensive spec requests
  • Implement alerts when credits fall below a threshold
  • Monitor credit usage patterns to plan purchases

Integration Patterns

  • Cache balance checks (don’t call on every request)
  • Update cache every 5-10 minutes
  • Use for decision logic in your application

Error Handling

  • Always handle network failures gracefully
  • Implement retry logic for transient errors
  • Provide meaningful error messages to users

HTTP Status Codes

CodeMeaningAction
200SuccessProcess the response
400Bad RequestCheck request parameters
401UnauthorizedVerify API key is valid and active
500Server ErrorContact support

Next: API Reference Overview

View all available API endpoints.

Authorizations

Authorization
string
header
default:YOUR_API_KEY
required

API key for authentication. Get your API key from https://pre.dev/projects/playground (Solo) or https://pre.dev/enterprise/dashboard?page=api (Enterprise). Use format: Bearer YOUR_API_KEY

Response

Credits balance retrieved successfully

success
boolean
required

Whether the request was successful

creditsRemaining
integer
required

The number of remaining credits available for the user