Defining actions
action
Define a public action function that can be called from clients.PropertyValidators
Argument validation object using validators from
convex/values.Validator
Return value validator.
function
required
internalAction
Define an internal action that can only be called from other Convex functions.Action context
ActionCtx
The context object passed to action handlers.function
Run a Convex query. Each call is a separate read transaction.Tip: Use
internalQuery to prevent users from calling the query directly.function
Run a Convex mutation. Each call is a separate write transaction.Tip: Use
internalMutation to prevent users from calling it directly.function
Run another Convex action.Important: Only use this when crossing runtimes (e.g., calling a
"use node" action from the default runtime). For code in the same runtime, extract shared logic into a TypeScript helper function instead.Scheduler
Auth
Authentication interface to get the current user’s identity.
StorageActionWriter
File storage interface with additional methods available only in actions.See Storage API for details.
function
Run a vector search on a table.
Common use cases
Calling external APIs
Actions can make HTTP requests to external services:Using Node.js libraries
Actions can use Node.js built-in modules and npm packages:Processing files
Actions can download, process, and re-upload files:Execution guarantees
At most once execution
Unlike mutations, actions are not automatically retried on transient errors. They execute at most once.
No direct database access
Actions cannot use
ctx.db. Use ctx.runQuery and ctx.runMutation instead.Non-deterministic operations allowed
Actions can call external APIs, use randomness, access the current time, and perform other non-deterministic operations.
Best practices
Use internal actions
For actions that should only be called from other functions, use
internalAction.Handle errors gracefully
Actions can fail due to network issues or external API errors. Always handle errors appropriately.
Keep actions idempotent
When possible, design actions so they can be safely retried without side effects.
Don't use runAction unnecessarily
Only use
runAction when crossing runtimes. For shared logic in the same runtime, use helper functions.