discover_api_endpoints
Monitor network requests on web pages to identify and document API endpoints for development analysis and integration.
Instructions
Discover API endpoints by monitoring network requests on a web page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | URL to analyze | |
| timeout | No | Analysis timeout in milliseconds |
Implementation Reference
- src/scrapers/api-scraper.ts:33-127 (handler)Core handler function that implements the logic for discovering API endpoints by launching a headless browser, navigating to the URL, monitoring network requests and responses, filtering API-like requests, extracting endpoints with parameters, and detecting authentication methods.async discoverAPIEndpoints(url: string, options?: { timeout?: number }): Promise<APIDiscoveryResult> { if (!Validators.isValidUrl(url)) { throw new Error('Invalid URL'); } const browser = await this.getBrowser(); const page = await browser.newPage(); const endpoints: APIEndpoint[] = []; const requests: NetworkRequest[] = []; try { // Monitor network requests page.on('request', (request) => { const requestUrl = request.url(); const method = request.method(); // Filter for API-like requests (JSON, XML, or common API patterns) if ( requestUrl.includes('/api/') || requestUrl.endsWith('.json') || requestUrl.includes('?') || method !== 'GET' ) { requests.push({ url: requestUrl, method, status: 0, statusText: '', headers: request.headers(), requestHeaders: request.headers(), }); } }); page.on('response', (response) => { const responseUrl = response.url(); const request = requests.find((r) => r.url === responseUrl); if (request) { request.status = response.status(); request.statusText = response.statusText(); request.type = response.headers()['content-type'] || ''; } }); // Navigate to page await page.goto(url, { waitUntil: 'networkidle', timeout: options?.timeout || 30000, }); // Wait a bit for all requests to complete await page.waitForTimeout(2000); // Process requests into endpoints for (const request of requests) { try { const urlObj = new URL(request.url); const method = request.method as APIEndpoint['method']; // Extract parameters from URL const parameters: APIEndpoint['parameters'] = []; urlObj.searchParams.forEach((_value, key) => { parameters.push({ name: key, type: 'string', required: false, location: 'query', }); }); endpoints.push({ method, path: urlObj.pathname, fullUrl: request.url, parameters: parameters.length > 0 ? parameters : undefined, statusCode: request.status, headers: request.headers, }); } catch { // Skip invalid URLs } } // Detect authentication const authInfo = this.detectAuthentication(requests); return { endpoints: [...new Map(endpoints.map((e) => [e.fullUrl, e])).values()], // Remove duplicates baseUrl: new URL(url).origin, authentication: authInfo, }; } finally { await page.close(); } }
- src/tools/api-discovery.ts:6-24 (registration)Tool registration entry in apiDiscoveryTools array defining the name, description, and input schema for the discover_api_endpoints tool.{ name: 'discover_api_endpoints', description: 'Discover API endpoints by monitoring network requests on a web page', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'URL to analyze', }, timeout: { type: 'number', description: 'Analysis timeout in milliseconds', default: 30000, }, }, required: ['url'], }, },
- src/tools/api-discovery.ts:9-23 (schema)Input schema definition for the discover_api_endpoints tool, specifying parameters url (required string) and optional timeout (number).inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'URL to analyze', }, timeout: { type: 'number', description: 'Analysis timeout in milliseconds', default: 30000, }, }, required: ['url'], },
- src/tools/api-discovery.ts:165-170 (handler)Wrapper handler in handleAPIDiscoveryTool that extracts parameters, calls the core APIScraper.discoverAPIEndpoints, formats the result, and returns it.case 'discover_api_endpoints': { const url = params.url as string; const timeout = params.timeout as number; const result = await apiScraper.discoverAPIEndpoints(url, { timeout }); return Formatters.formatAPIDiscoveryResults(result); }