addCardsBatch
Add multiple flashcards to an Anki deck simultaneously to save time when creating study materials.
Instructions
Add multiple cards to a deck in batch
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cards | Yes | Array of cards to add | |
| deckId | Yes | ID of the deck to add cards to |
Implementation Reference
- src/index.ts:119-153 (registration)Registration of the addCardsBatch tool, including the handler execute function and input schema using server.addTool.server.addTool({ description: 'Add multiple cards to a deck in batch', execute: async args => { try { const data = await ankiApiRequest( 'POST', `/api/v1/decks/${args.deckId}/cards/batch`, { cards: args.cards, }, ); return JSON.stringify(data, null, 2); } catch (error) { return `Error: ${error instanceof Error ? error.message : String(error)}`; } }, name: 'addCardsBatch', parameters: z.object({ cards: z .array( z.object({ back: z.string().describe('Back side of the card'), cardType: z.enum(['BASIC', 'CLOZE']).describe('Type of card'), clozeText: z .string() .optional() .describe('Cloze text for cloze cards'), front: z.string().describe('Front side of the card'), tags: z.array(z.string()).optional().describe('Tags for the card'), }), ) .describe('Array of cards to add'), deckId: z.string().describe('ID of the deck to add cards to'), }), });
- src/index.ts:121-134 (handler)The execute handler for addCardsBatch tool that sends a POST request to the Anki API endpoint /api/v1/decks/{deckId}/cards/batch with the cards data.execute: async args => { try { const data = await ankiApiRequest( 'POST', `/api/v1/decks/${args.deckId}/cards/batch`, { cards: args.cards, }, ); return JSON.stringify(data, null, 2); } catch (error) { return `Error: ${error instanceof Error ? error.message : String(error)}`; } },
- src/index.ts:136-152 (schema)Zod schema defining the input parameters for the addCardsBatch tool: deckId (string) and cards (array of card objects).parameters: z.object({ cards: z .array( z.object({ back: z.string().describe('Back side of the card'), cardType: z.enum(['BASIC', 'CLOZE']).describe('Type of card'), clozeText: z .string() .optional() .describe('Cloze text for cloze cards'), front: z.string().describe('Front side of the card'), tags: z.array(z.string()).optional().describe('Tags for the card'), }), ) .describe('Array of cards to add'), deckId: z.string().describe('ID of the deck to add cards to'), }),
- src/index.ts:23-63 (helper)Shared helper function used by addCardsBatch (and other tools) to make authenticated API requests to the Anki server.async function ankiApiRequest( method: string, endpoint: string, body?: Record<string, unknown>, queryParams?: Record<string, boolean | number | string | undefined>, ) { const url = new URL(`${ANKI_BASE_URL}${endpoint}`); // Add query parameters if provided if (queryParams) { Object.entries(queryParams).forEach(([key, value]) => { if (value !== undefined && value !== '') { url.searchParams.append(key, String(value)); } }); } const options: RequestInit = { headers: { Authorization: `Bearer ${ANKI_API_KEY}`, 'Content-Type': 'application/json', }, method, }; if (body && method !== 'GET') { options.body = JSON.stringify(body); } const response = await fetch(url.toString(), options); const data = (await response.json()) as unknown; if (!response.ok) { const errorData = data as { error?: { message?: string } }; throw new Error( errorData.error?.message || `API request failed: ${response.statusText}`, ); } return data; }