Skip to content

Latest commit

 

History

History
113 lines (86 loc) · 3.01 KB

File metadata and controls

113 lines (86 loc) · 3.01 KB

Quota Management

API methods cost: 0 (free), 10 (standard), or 100 (resource-intensive) units.

Check Quota

import { SocketSdk } from '@socketsecurity/sdk'

const client = new SocketSdk('your-api-key')
const quota = await client.getQuota()

if (quota.success) {
  console.log(`Available: ${quota.data.quota} units`)
}

Utilities

import {
  getQuotaCost,
  calculateTotalQuotaCost,
  hasQuotaForMethods,
  getMethodsByQuotaCost,
} from '@socketsecurity/sdk'

// Get method cost
getQuotaCost('batchPackageFetch') // 100
getQuotaCost('getOrgAnalytics') // 10
getQuotaCost('getQuota') // 0

// Calculate total
const cost = calculateTotalQuotaCost([
  'batchPackageFetch', // 100
  'getOrgAnalytics', // 10
  'getQuota', // 0
]) // Returns: 110

// Check quota
const canProceed = hasQuotaForMethods(availableQuota, [
  'batchPackageFetch',
  'createFullScan',
])

// Methods by cost
getMethodsByQuotaCost(0) // Free methods
getMethodsByQuotaCost(10) // Standard methods
getMethodsByQuotaCost(100) // Expensive methods

Examples

Pre-flight Check

const operations = ['batchPackageFetch', 'uploadManifestFiles']
const required = calculateTotalQuotaCost(operations)

const quota = await client.getQuota()
if (!quota.success || !hasQuotaForMethods(quota.data.quota, operations)) {
  throw new Error(`Need ${required} units, have ${quota.data.quota}`)
}

Monitor Usage

class QuotaTracker {
  private used = 0

  async track<T>(methodName: string, op: () => Promise<T>): Promise<T> {
    const cost = getQuotaCost(methodName)
    const result = await op()
    this.used += cost
    console.log(`Used ${this.used} units`)
    return result
  }
}

Fallback Strategy

const quota = await client.getQuota()
const batchCost = getQuotaCost('batchPackageFetch')

if (quota.success && quota.data.quota >= batchCost) {
  await client.batchPackageFetch({ components })
} else {
  // Fall back to individual queries
  for (const pkg of packages) {
    await client.getScoreByNpmPackage(pkg.name, pkg.version)
  }
}

Cost Reference

For the complete list of API method quota costs, see data/api-method-quota-and-permissions.json.

Summary:

  • Free (0): 44 methods including getQuota, getOrganizations, getEntitlements, createFullScan, getScan, getScanList, getOrgSecurityPolicy, updateOrgSecurityPolicy, repo management, triage, labels, diff scans, exports, and more
  • Standard (10): getOrgAnalytics, getRepoAnalytics, getAuditLogEvents, getIssuesByNpmPackage, getScoreByNpmPackage, getOrgAlertFullScans, API token operations
  • Expensive (100): batchPackageFetch, batchOrgPackageFetch, batchPackageStream, createDependenciesSnapshot, createScanFromFilepaths, searchDependencies, uploadManifestFiles

Best Practices

  • Check quota before expensive operations
  • Use batching (100 units for all vs 10 per package)
  • Monitor usage with tracker
  • Implement fallback strategies