Quarto API and @quarto/types
Scope of this document
This document covers adding or modifying methods in existing namespaces of the Quarto API.
Out of scope: Creating new namespaces, renaming namespaces, or restructuring the API architecture. These operations require reading src/core/api/*.ts in depth and should be planned and executed with a human in the loop.
Building @quarto/types
To build the @quarto/types package:
This runs typecheck and then bundles all type definitions into dist/index.d.ts.
Updating the Quarto API
The Quarto API is how external execution engines access Quarto's core functionality. The API exists in two places:
Type definitions in
packages/quarto-types/- consumed by external engines (TypeScript)Implementation in
src/core/api/- used within quarto-cli
Existing namespaces
The API has these namespaces (each has a corresponding file in src/core/api/):
system- Process execution, environment detection, temp filesconsole- Logging, spinners, user feedbackpath- Path manipulation and resource locationsformat- Format detection utilitiesjupyter- Jupyter notebook operationstext- Text processing utilitiesmappedString- Source-mapped string operationsmarkdownRegex- Markdown parsing with regexcrypto- Cryptographic utilities
Step-by-step: Adding a method to an existing namespace
Follow these steps when adding a new method to an existing namespace:
1. Add to quarto-types type definitions
In packages/quarto-types/src/quarto-api.ts, find the namespace and add your method:
If your method needs new types, add them to the appropriate file in packages/quarto-types/src/ (e.g., system.ts for system-related types).
2. Test the type definitions
This will typecheck and bundle. Fix any errors before proceeding.
3. Add to internal types
In src/core/api/types.ts, find the namespace interface and add your method:
4. Implement the method
In the namespace's implementation file (e.g., src/core/api/system.ts):
Import any needed functions at the top
Add the method to the returned object
5. Verify with typecheck
No output means success.
6. Commit with built artifact
Always commit the built dist/index.d.ts file along with source changes:
Using the Quarto API in source files
Execution engines (preferred)
Execution engines receive the API via their init() method. Store it for use throughout the engine:
When init() API is not available
For helper modules that don't have access to the API via init(), use getQuartoAPI():
Removing old imports
When moving functionality to the API, remove direct imports from internal modules:
This ensures external engines and internal code use the same interface.