Tutorials & Guides
How to Build a Local CLI Tool to Convert Raw JSON Payloads into TypeScript Interfaces with Claude 3.5 Sonnet
Tired of manually typing deeply nested API responses? Build a blazing-fast local Node.js CLI tool that grabs raw JSON from your clipboard and outputs clean, commented TypeScript interfaces using Claude 3.5 Sonnet.
Updated 11/9/2026
We have all been there: you are integrating a third-party API that has either non-existent documentation or documentation written in 2017. You fire off a curl request, get a massive, deeply nested JSON blob back, and sigh. Now you have to spend the next twenty minutes manually writing TypeScript interfaces, guessing which fields are optional, and figuring out what to name that highly specific nested array of nested objects.
Sure, there are automated tools out there more basic than our AI overlords—quicktype is fine, but it lacks raw structural intelligence. It does not understand context. It names your nested interfaces things like Datum or Element3. It has no idea that created_at should probably be handled as an ISO string, or that a nullable field called last_login_ip needs a specific JSDoc comment pointing out its deprecated status.
We can do better. Let's build a local CLI tool in Node.js that takes a raw JSON payload from your clipboard, passes it to Claude 3.5 Sonnet, and spits out clean, fully commented, idiomatic TypeScript interfaces.
Why Claude 3.5 Sonnet?
There are plenty of LLMs on the market, but Sonnet 3.5 is the undisputed champion of structured code synthesis. It understands TypeScript type definitions better than most junior developers, and it has an uncanny ability to infer engineering intent from object keys. If it sees status: 'active' | 'suspended', it won't just type it as a generic string; it will capture it as a clean union type.
To get this tool running smoothly, we will use the official Anthropic SDK and build a lean, execution-focused Node script. If you hit any snags with API keys, rate limits, or connection timeouts, check out our troubleshooting guide for Claude developer tools.
Which AI should I use for this?
Tickd is independent and not affiliated with any AI company.
Step 1: Setting Up the Local Project
Let's keep this lightweight. No massive build setups or compilation steps. We will write a raw Node.js script using ESM modules.
First, initialise your project and install the dependencies:
`bash
mkdir json-to-ts-cli
cd json-to-ts-cli
npm init -y
npm install @anthropic-ai/sdk dotenv clipboardy
`
We are using clipboardy to read directly from your system clipboard. This means you can copy a JSON payload from your browser's network tab or Postman, run the command, and instantly have your types ready to paste into your codebase. No tedious manual saving required.
Add "type": "module" to your package.json to enable modern import syntax.
Create a .env file to house your Anthropic API key:
`env
ANTHROPIC_API_KEY=your_actual_api_key_here
`
Step 2: Crafting the System Prompt
To make this tool reliable, we need to instruct Claude to output only TypeScript. We do not want markdown backticks, conversational preambles, or polite "Sure, here are your interfaces!" notes. We want code we can pipe directly into a file if we choose to.
You can generate complex prompt variations with our prompt generator tool, but for this specific CLI, we will inject a highly deterministic system prompt directly into the script.
Here is the prompt logic we will use:
- Output ONLY raw TypeScript. No chatty markdown wraps.
- Infer correct types (e.g., convert timestamp strings into appropriate types or add JSDoc comments).
- Parse nested objects into dedicated, logically named interfaces instead of using inline object typing.
- Identify and flag nullable or optional fields.
Step 3: Writing the CLI Script
Now, create a file named index.js. This script will pull the JSON from the clipboard, validate that it is actual JSON (to avoid sending garbage to the Anthropic API), trigger the model, and print the output.
`javascript
#!/usr/bin/env node
import Anthropic from '@anthropic-ai/sdk'; import clipboard from 'clipboardy'; import dotenv from 'dotenv';
dotenv.config();
if (!process.env.ANTHROPIC_API_KEY) { console.error('Error: ANTHROPIC_API_KEY is not defined in your environment.'); process.exit(1); }
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, });
async function generateTypes() { let rawClipboard; try { rawClipboard = await clipboard.read(); } catch (err) { console.error('Failed to read from clipboard:', err.message); process.exit(1); }
// Quick sanity check: Is this clipboard content actually JSON? try { JSON.parse(rawClipboard); } catch (e) { console.error('Error: Clipboard content is not valid JSON. Please copy a valid JSON payload first.'); process.exit(1); }
console.log('Synthesising TypeScript interfaces...');
try {
const response = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 4000,
system: `You are an expert TypeScript engineer. Your sole task is to convert the provided JSON payload into clean, idiomatic TypeScript interfaces.
Follow these rules strictly:
1. Output ONLY the raw TypeScript interfaces. Do not include markdown blocks, introductory text, or concluding remarks.
2. Infer correct types. Convert timestamp strings into appropriate types or add JSDoc comments indicating they are ISO strings.
3. Look for patterns in nested objects to extract them into dedicated, logically named interfaces instead of using inline objects.
4. Make fields optional (using '?') if they are null or missing in portions of the schema, or if their naming suggests optionality.
5. Avoid 'any' at all costs. Use unknown or union types if a type cannot be strictly determined.`,
messages: [
{
role: 'user',
content: Please convert this JSON payload: \n\n${rawClipboard}
}
]
});
const output = response.content[0].text.trim(); console.log('\n--- Synthesised TypeScript Interfaces ---\n'); console.log(output); console.log('\n----------------------------------------\n');
// Auto-copy the output back to clipboard for speed await clipboard.write(output); console.log('Copied interfaces back to your clipboard! Ready to paste.');
} catch (error) { console.error('Error communicating with Claude:', error.message); } }
generateTypes();
`
Step 4: Making It Global
Having to navigate to this specific directory to run the script defeats the purpose of a fast developer workflow. Let's make it a global terminal command.
Add a bin property to your package.json:
`json
"bin": {
"jsontots": "./index.js"
}
`
Now, from inside your project folder, link the package globally:
`bash
npm link
`
Now, wherever you are in your terminal, you can run jsontots. The CLI will instantly read your clipboard, talk to Sonnet, spit out beautiful TypeScript interfaces, and copy them right back to your clipboard so you can paste them straight into your editor.
Why This Workflow Wins
What makes this tool tick is the removal of friction. No context switching, no pasting messy payloads into browser tabs, and no manually stripping away unwanted markdown explanations. Claude 3.5 Sonnet's fast inference speeds mean you get highly accurate, context-aware types in under five seconds.
If you are building larger enterprise workflows, you can easily extend this script to read a local directory of raw response mocks and batch-export them directly into .d.ts declaration files. Go forth and type-safe your codebase without the manual slog.
Keep going
Build something with the prompt generator, decode the jargon in the glossary, or compare the tools on our platform deep-dives.