title: Enqueue Workflows from External Applications impact: MEDIUM impactDescription: Enables external services to submit work to DBOS queues tags: client, enqueue, external, queue
Enqueue Workflows from External Applications
Use client.enqueue() to submit workflows from outside your DBOS application. Since DBOSClient runs externally, workflow and queue metadata must be specified explicitly.
Incorrect (trying to use DBOS.startWorkflow from external code):
// DBOS.startWorkflow requires a full DBOS setup
await DBOS.startWorkflow(processTask, { queueName: "myQueue" })("data");
Correct (using DBOSClient.enqueue):
import { DBOSClient } from "@dbos-inc/dbos-sdk";
const client = await DBOSClient.create({
systemDatabaseUrl: process.env.DBOS_SYSTEM_DATABASE_URL!,
// The application that owns and runs the workflows (set if apps share a system database)
applicationName: "my-app",
});
// Optionally register the queue from the client (persists to system database)
await client.registerQueue("task_queue", { globalConcurrency: 10 });
// Basic enqueue
const handle = await client.enqueue(
{
workflowName: "processTask",
queueName: "task_queue",
},
"task-data"
);
// Wait for the result
const result = await handle.getResult();
The queue must be registered (with DBOS.registerQueue or client.registerQueue) for the workflow to run. A workflow enqueued on an unregistered queue is durably recorded as ENQUEUED and stays there until the queue is registered and a worker becomes available.
Type-safe enqueue:
// Import or declare the workflow type
declare class Tasks {
static processTask(data: string): Promise<string>;
}
const handle = await client.enqueue<typeof Tasks.processTask>(
{
workflowName: "processTask",
workflowClassName: "Tasks",
queueName: "task_queue",
},
"task-data"
);
// TypeScript infers the result type
const result = await handle.getResult(); // type: string
Enqueue options:
workflowName(required): Name of the workflow functionqueueName(required): Name of the queueworkflowClassName: Class name if the workflow is a class methodworkflowConfigName: Instance name if usingConfiguredInstanceworkflowID: Custom workflow IDworkflowTimeoutMS: Timeout in millisecondsdeduplicationID: Prevent duplicate enqueuespriority: Queue priority (lower = higher priority)delaySeconds: Delay before becoming eligible for executionqueuePartitionKey: Partition key for partitioned queuesappVersion: Pin the workflow to a specific application version. If unset, the workflow is only dequeued by an executor running the application’s latest registered version, and takes that executor’s version when dequeuedduplicationPolicy: How to handle adeduplicationIDcollision.'reject'(default) throwsDBOSQueueDuplicatedError;'return-existing'attaches to the existing workflow and returns its handle (singleton pattern — requiresdeduplicationID)applicationName: Application that owns and runs the workflow (defaults to the client’sapplicationName)serializationType: Serialization strategy for workflow arguments ("portable"for cross-language interop, otherwise the configured serializer is used)
Singleton workflow example (return-existing):
const handle = await client.enqueue(
{
workflowName: "processTask",
queueName: "task_queue",
deduplicationID: "singleton",
duplicationPolicy: "return-existing",
},
"task-data"
);
// If a workflow with deduplicationID "singleton" is already enqueued or
// running on this queue, handle resolves to that workflow's result instead
// of throwing.
Cross-language enqueue (serializationType: "portable"):
await client.enqueue(
{
workflowName: "processOrder",
queueName: "orders",
serializationType: "portable", // Python/Java/Go workers can read these args
},
"order-123"
);Enqueueing Atomically With Your Own Writes
client.enqueueInTransaction performs the enqueue inside a transaction you own, so the workflow is enqueued if and only if your database writes commit. Pass a node-postgres Client or PoolClient with an open transaction, connected to the DBOS system database:
import { Client } from "pg";
declare class Orders {
static processOrder(orderId: string): Promise<void>;
}
const orderId = "order-123";
const pg = new Client({ connectionString: process.env.DBOS_SYSTEM_DATABASE_URL });
await pg.connect();
let handle;
try {
await pg.query("BEGIN");
await pg.query("INSERT INTO orders (id, status) VALUES ($1, 'new')", [orderId]);
handle = await client.enqueueInTransaction<typeof Orders.processOrder>(
pg,
{ workflowName: "processOrder", workflowClassName: "Orders", queueName: "orders" },
orderId,
);
await pg.query("COMMIT"); // The workflow does not exist until this commits
} catch (e) {
await pg.query("ROLLBACK"); // Neither the row nor the workflow is created
throw e;
} finally {
await pg.end();
}
await handle.getResult(); // Only call getResult() after the commit
- You own the transaction: DBOS never begins, commits, rolls back, or retries it
duplicationPolicy: 'return-existing'is not supported (throws)client.enqueuePortableInTransaction(pg, options, positionalArgs, namedArgs?)is the portable-serialization variant (for targets with named arguments, e.g. Python kwargs);client.enqueuePortable(options, positionalArgs, namedArgs?)is the non-transactional oneclient.sendInTransaction(pg, destinationID, message, topic?, idempotencyKey?)sends a message atomically in the same way (seecomm-messages.md)
Always call client.destroy() when done.
Reference: DBOS Client Enqueue