Type Generation
The promptly generate CLI command fetches all your prompts from the API and generates a TypeScript declaration file with typed template variables for every prompt ID.
Run codegen
Section titled “Run codegen”bunx promptly generatenpx promptly generateyarn dlx promptly generatepnpm dlx promptly generateThis generates a promptly-env.d.ts declaration file. The CLI automatically places it in src/types/ or types/ if either directory exists in your project, otherwise it falls back to the project root.
Generated output
Section titled “Generated output”The generated file uses declaration merging to augment the PromptVariableMap interface:
// Auto-generated by @promptlycms/prompts - do not editimport '@promptlycms/prompts';
declare module '@promptlycms/prompts' { interface PromptVariableMap { 'review-prompt': { [V in 'latest' | '2.0.0' | '1.0.0']: { pickupLocation: string; items: string; }; }; 'welcome-email': { [V in 'latest' | '1.0.0']: { email: string; subject: string; }; }; }}What this gives you
Section titled “What this gives you”With the generated file present, you get:
- Prompt ID autocomplete -
getPrompt('suggests all known prompt IDs - Typed template variables -
result.userMessage({ ... })only accepts the correct variable keys - Version-aware types - different versions can have different variables, and the types track this
- Fallback for unknown IDs - untyped prompt IDs fall back to
Record<string, string>
Before codegen
Section titled “Before codegen”// No autocomplete, no type checking on variablesconst result = await promptly.getPrompt('review-prompt');result.userMessage({ anything: 'goes' }); // No errorAfter codegen
Section titled “After codegen”// Autocomplete for prompt ID and variablesconst result = await promptly.getPrompt('review-prompt');
result.userMessage({ pickupLocation: 'London', // Typed items: 'sofa', // Typed});
result.userMessage({ wrong: 'key', // Type error});Output location
Section titled “Output location”By default, the CLI auto-detects the best location for the generated file:
| Priority | Directory | Output path |
|---|---|---|
| 1 | src/types/ exists | src/types/promptly-env.d.ts |
| 2 | types/ exists | types/promptly-env.d.ts |
| 3 | Fallback | ./promptly-env.d.ts |
To override, use the --output (-o) flag:
npx promptly generate --output ./custom/path/promptly-env.d.tsPass API key directly
Section titled “Pass API key directly”If you don’t have PROMPTLY_API_KEY set in your environment:
npx promptly generate --api-key pk_live_...Best practices
Section titled “Best practices”- Re-run codegen whenever you add, remove, or rename template variables in the CMS
- Commit the generated file so types are available without running codegen
- Add to your build script if you want to always regenerate on deploy
How it works
Section titled “How it works”Under the hood, promptly generate:
- Reads
PROMPTLY_API_KEYfrom the environment (or--api-keyflag) - Calls
GET /prompts?include_versions=trueto fetch all prompts - Extracts
${variable}template patterns from each prompt’suserMessage - Groups versions with identical variables together
- Writes a
declare module '@promptlycms/prompts'augmentation file
The PromptVariableMap interface is intentionally empty in the source package - it exists only as a declaration merging target. This is the same pattern used by Prisma and GraphQL Code Generator.
Next steps
Section titled “Next steps”- Learn about fetching prompts with typed results
- See the full CLI reference for all flags