### API Caching Example with Curl Source: https://cursor.com/docs/api Demonstrates how to use curl to interact with an API endpoint, first making an initial request and then a subsequent request including the ETag for caching. ```bash # Initial request curl -X GET "https://api.cursor.com/analytics/team/dau" \ -H "Authorization: Bearer YOUR_API_KEY" \ -D headers.txt # Response includes: ETag: "abc123xyz" # Subsequent request with ETag curl -X GET "https://api.cursor.com/analytics/team/dau" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "If-None-Match: \"abc123xyz\"" # Returns 304 Not Modified if data hasn't changed ``` -------------------------------- ### Rate Limit Response Example Source: https://cursor.com/docs/api This is an example of a JSON response received when the API rate limit is exceeded. It indicates that too many requests have been made and suggests retrying later. ```json { "error": "Too Many Requests", "message": "Rate limit exceeded. Please try again later." } ``` -------------------------------- ### Basic Authentication with Authorization Header Source: https://cursor.com/docs/api Alternatively, set the Authorization header directly using a base64 encoded string of your API key followed by a colon. ```bash Authorization: Basic {base64_encode('YOUR_API_KEY:')} ``` -------------------------------- ### Basic Authentication with Curl Source: https://cursor.com/docs/api Use your API key as the username in basic authentication with an empty password when making requests to Cursor APIs. ```bash curl https://api.cursor.com/teams/members \ -u YOUR_API_KEY: ``` -------------------------------- ### 404 Not Found Error Response Source: https://cursor.com/docs/api This JSON structure represents a 'Not Found' error, indicating that the requested resource does not exist. ```json { "error": "Not Found", "message": "Resource not found" } ``` -------------------------------- ### Handle API Errors in JavaScript Source: https://cursor.com/docs/api Implement try-catch blocks to manage potential errors during API requests. This snippet specifically checks for rate limiting (429), authentication failures (401), insufficient permissions (403), and general API errors. ```javascript async function fetchAnalytics(endpoint) { try { const response = await fetch(`https://api.cursor.com${endpoint}`, { headers: { 'Authorization': `Basic ${btoa(API_KEY + ':')}` } }); if (response.status === 429) { // Rate limited - implement backoff throw new Error('Rate limit exceeded'); } if (response.status === 401) { // Invalid API key throw new Error('Authentication failed'); } if (response.status === 403) { // Insufficient permissions throw new Error('Enterprise access required'); } if (!response.ok) { throw new Error(`API error: ${response.status}`); } return await response.json(); } catch (error) { console.error('API request failed:', error); throw error; } } ``` -------------------------------- ### Implement Exponential Backoff for API Retries Source: https://cursor.com/docs/api This Python function demonstrates how to implement exponential backoff when making API requests. It retries requests that receive a 429 status code with increasing wait times. ```python import time import requests def make_request_with_backoff(url, headers, max_retries=5): for attempt in range(max_retries): response = requests.get(url, headers=headers) if response.status_code == 429: # Exponential backoff: 1s, 2s, 4s, 8s, 16s wait_time = 2 ** attempt print(f"Rate limited. Waiting {wait_time}s before retry...") time.sleep(wait_time) continue return response raise Exception("Max retries exceeded") ``` -------------------------------- ### Bearer Authentication for Cloud Agents API Source: https://cursor.com/docs/api The Cloud Agents API supports Bearer token authentication. Use your API key as the token in the Authorization header. ```bash curl https://api.cursor.com/v1/me \ -H "Authorization: Bearer YOUR_API_KEY" ``` -------------------------------- ### 400 Bad Request Error Response Source: https://cursor.com/docs/api This JSON structure represents a 'Bad Request' error, typically indicating invalid or missing request parameters. ```json { "error": "Bad Request", "message": "Some users are not in the team" } ``` -------------------------------- ### 500 Internal Server Error Response Source: https://cursor.com/docs/api This JSON structure represents a 'Internal Server Error', indicating a problem on the server side. Contact support if the issue persists. ```json { "error": "Internal Server Error", "message": "An unexpected error occurred" } ``` -------------------------------- ### 401 Unauthorized Error Response Source: https://cursor.com/docs/api This JSON structure indicates an 'Unauthorized' error, usually due to an invalid or missing API key. ```json { "error": "Unauthorized", "message": "Invalid API key" } ``` -------------------------------- ### 403 Forbidden Error Response Source: https://cursor.com/docs/api This JSON structure signifies a 'Forbidden' error, meaning the API key is valid but lacks the necessary permissions, such as requiring Enterprise features. ```json { "error": "Forbidden", "message": "Enterprise access required" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.