Code Tool
The Code Tool allows you to write and execute custom TypeScript code that runs when your assistant needs to perform a specific action. Unlike custom function tools that require you to host a server, code tools run directly on Vapi’s infrastructure.
When to Use Code Tools
Code tools are ideal when you need to:
- Transform or process data during a conversation
- Make HTTP requests to external APIs
- Perform calculations or business logic
- Avoid the overhead of setting up and maintaining a webhook server
Creating a Code Tool
Step 1: Navigate to the Tools Section
- Open your Vapi Dashboard
- Click Tools in the left sidebar
- Click Create Tool and select Code
Step 2: Configure Your Code Tool
The dashboard provides a visual interface to configure your code tool:
- Tool Name: A descriptive identifier (e.g.,
get_customer_data) - Description: Explain what your tool does - this helps the AI understand when to use it
- TypeScript Code: Write the code that will execute when the tool is called
- Parameters: Define the input parameters your code expects
- Environment Variables: Store sensitive values like API keys securely
Step 3: Write Your Code
Your code has access to two objects:
args: Contains the parameters passed by the assistantenv: Contains your environment variables
Your code runs in an isolated environment with a configurable timeout (default: 10 seconds, max: 60 seconds).
Example: Customer Lookup Tool
Let’s create a tool that looks up customer information:
Configuration
Parameters
Environment Variables
Code
Example: Order Processing Tool
A more complex example that processes an order:
Parameters
Code
Using Code Tools in Assistants
Once created, add your code tool to any assistant:
In the Dashboard
- Go to Assistants → Select your assistant
- Navigate to the Tools tab
- Click Add Tool and select your code tool
- Save your assistant configuration
Via API
Creating Code Tools via API
You can also create code tools programmatically:
Best Practices
Security
- Store sensitive values (API keys, secrets) in Environment Variables, not in your code
- Environment variable values support Liquid templates to reference call variables
Performance
- Keep code execution under the timeout limit
- Use efficient API calls and avoid unnecessary loops
- Consider caching strategies for repeated lookups
Error Handling
- Always handle potential errors from API calls
- Return meaningful error messages that help the assistant respond appropriately
Return Values
- Return structured data that the assistant can easily interpret
- Include relevant information the assistant needs to continue the conversation
Limitations
- Timeout: Maximum execution time is 60 seconds (default: 10 seconds)
- No file system access: Code runs in an isolated environment without file access
- Memory: Code runs with limited memory allocation
- Network: Only outbound HTTP/HTTPS requests are supported
Code Tool vs Custom Function Tool
Choose Code Tools when you want to quickly add functionality without managing infrastructure. Choose Custom Function Tools when you need full control over the execution environment or have existing server infrastructure.