iaptic_switch_app
Switch to a different Iaptic app by specifying the app name and optional API key. Subsequent API calls use the new app's credentials.
Instructions
Switch to a different Iaptic app.
Allows temporarily using a different app's credentials
All subsequent API calls will use the new app name and API key
Useful for managing multiple apps in the same session
Required: appName parameter (apiKey required only if not using master key)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| appName | Yes | Name of the app to switch to | |
| apiKey | No | API key for the app (not required if using master key) |
Implementation Reference
- src/tools/app.ts:65-99 (handler)Handler for the 'iaptic_switch_app' tool. Routes to the switchApp method on the IapticAPI class, handling both master-key and app-specific API key scenarios. Returns success message or error if apiKey is missing when not using a master key.
async handleTool(name: string, args: any) { switch (name) { case 'iaptic_switch_app': { const appInfo = this.api.getCurrentAppInfo(); // If using master key, apiKey is not required if (appInfo.usingMasterKey && !args.apiKey) { this.api.switchApp('dummy-api-key', args.appName); return { content: [{ type: "text", text: `Successfully switched to app: ${args.appName}` }] }; } // Otherwise, apiKey is required if (!args.apiKey) { return { isError: true, content: [{ type: "text", text: "Error: apiKey parameter is required when not using a master key" }] }; } this.api.switchApp(args.apiKey, args.appName); return { content: [{ type: "text", text: `Successfully switched to app: ${args.appName}` }] }; } - src/tools/app.ts:17-38 (schema)Input schema definition for 'iaptic_switch_app' tool. Requires appName (string) and optionally apiKey (string). Includes description explaining the tool allows switching to a different Iaptic app's credentials.
{ name: "iaptic_switch_app", description: `Switch to a different Iaptic app. - Allows temporarily using a different app's credentials - All subsequent API calls will use the new app name and API key - Useful for managing multiple apps in the same session - Required: appName parameter (apiKey required only if not using master key)`, inputSchema: { type: "object", properties: { appName: { type: "string", description: "Name of the app to switch to" }, apiKey: { type: "string", description: "API key for the app (not required if using master key)" } }, required: ["appName"] } }, - src/iaptic-api.ts:140-152 (helper)Core implementation of switchApp() on IapticAPI. If master key is set, only updates appName; otherwise updates both apiKey and appName. Then re-initializes the HTTP client with new credentials.
// Method to switch to a different app switchApp(apiKey: string, appName: string): void { // If we have a master key, we only need to update the appName if (this.masterKey) { this.appName = appName; console.error(`Switched to app: ${appName} (using master key)`); } else { this.apiKey = apiKey; this.appName = appName; console.error(`Switched to app: ${appName}`); } this.initializeClient(); } - src/server.ts:68-110 (registration)Registration of all tools, including iaptic_switch_app, via ListToolsRequestSchema. Tool calls starting with 'iaptic_' are routed to AppTools.handleTool() in the CallToolRequestSchema handler.
private setupHandlers() { // List available tools this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ ...this.tools.customers.getTools(), ...this.tools.purchases.getTools(), ...this.tools.transactions.getTools(), ...this.tools.statistics.getTools(), ...this.tools.stripe.getTools(), ...this.tools.events.getTools(), ...this.tools.app.getTools() ] }; }); // Handle tool calls this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { // Route tool calls to appropriate handler if (name.startsWith('customer_')) { return await this.tools.customers.handleTool(name, args); } if (name.startsWith('purchase_')) { return await this.tools.purchases.handleTool(name, args); } if (name.startsWith('transaction_')) { return await this.tools.transactions.handleTool(name, args); } if (name.startsWith('stats_')) { return await this.tools.statistics.handleTool(name, args); } if (name.startsWith('stripe_')) { return await this.tools.stripe.handleTool(name, args); } if (name.startsWith('event_')) { return await this.tools.events.handleTool(name, args); } if (name.startsWith('iaptic_')) { return await this.tools.app.handleTool(name, args); } - src/iaptic-api.ts:99-138 (helper)initializeClient() recreates the HTTP client with updated auth token (appName + apiKey or masterKey), used by switchApp() after changing credentials.
private initializeClient() { const authToken = this.masterKey ? Buffer.from(`${this.appName}:${this.masterKey}`).toString('base64') : Buffer.from(`${this.appName}:${this.apiKey}`).toString('base64'); this.client = axios.create({ baseURL: 'https://validator.iaptic.com/v3', headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' } }); // Add request interceptor to log curl command this.client.interceptors.request.use(request => { const method = (request.method || 'GET').toUpperCase(); const url = (request.baseURL || '') + (request.url || ''); const headers = Object.entries(request.headers) .map(([key, value]) => `-H '${key}: ${value}'`) .join(' '); const data = request.data ? `-d '${JSON.stringify(request.data)}'` : ''; const params = request.params ? '?' + new URLSearchParams(request.params).toString() : ''; console.error(`curl -X ${method} '${url}${params}' ${headers} ${data}`); return request; }); // Add response interceptor for error handling this.client.interceptors.response.use( response => response, (error: AxiosError<IapticErrorResponse>) => { if (error.response?.data) { const { message, code, status } = error.response.data; throw new Error(`Iaptic API Error (${status || code}): ${message || 'Unknown error'}`); } throw error; } ); }