Skip to content

Create a dispatch job from a function

A function can create a dispatch job directly, without holding a platform credential. It names a target function of its own application and a webhook path on it; the platform builds the URL, signs the delivery and runs it through the normal delivery pipeline, so you get retries, ordering by message group, a visible job in the console and the ability to pause it.

Use it when one function should hand slow or retryable work to another, or fan work out under a connection that can be paused.

A dispatch is checked against what the function declared, so declare it first. A declaration carries the target, the path, a connection (required) and optionally a dispatch pool:

func init() {
fn.Dispatches("yard.logger", "/deliver",
fn.DispatchConnection("yard-conn"), // required: Dispatches panics at registration without it
fn.DispatchPool("yard-process-pool")) // optional
}
  • target is a function address in the same application: the first label must be your application’s code. A target is never a URL: the job is signed by an application’s credential, and a URL would send that credential wherever the function pointed.
  • path is a literal absolute path (no { } * ? #, no %, no //, no . or .. segments) that lands on a webhook endpoint of the target. A (target, path) pair may not repeat.
  • connection is the code of the connection the jobs go through. It is required: the job carries it, so pausing the connection holds the jobs PENDING, and the connection’s service account signs the delivery. See Subscriptions and connections. That account must be one the function may sign as: an account that belongs to the function’s application, or the function’s own service account. A shared (application-less) connection’s account, or one of another application, is refused (DISPATCH_SIGNER_NOT_ALLOWED, below).
  • pool is the code of a dispatch pool. It is optional; without it the platform chooses.

The connection and pool are the declaration’s, not the call’s: the call cannot override them. The platform resolves them on every call. An unknown pool or connection is BAD_REQUEST; a suspended or foreign pool is NOT_ALLOWED; a paused connection still takes the job and holds it.

jobID, err := fn.Dispatch(r.Context(), fn.Job{
Target: "yard.logger",
Path: "/deliver",
Payload: []byte(`{"id":42}`),
Metadata: map[string]string{"from": "trip-processor"},
MessageGroup: "trip-42",
})
if err != nil {
if errors.Is(err, fn.ErrRetryable) {
fn.Retry(w, 5*time.Second)
return
}
fn.Reject(w, "dispatch refused")
return
}

Only target and path are required. The rest mirror the dispatch-job fields:

Field Default
code <application>:function:<name>:dispatch. If set it must start with the application code, no whitespace, at most 255 characters
payload delivered as the request body. UTF-8, at most 256 KiB, valid JSON when the content type is JSON
contentType application/json
dataOnly true: the payload alone is the body; false wraps it in the platform’s envelope
messageGroup, mode none; mode is IMMEDIATE, NEXT_ON_ERROR or BLOCK_ON_ERROR
correlationId the calling request’s X-Correlation-Id
causationId, subject, descriptor none; descriptor says what the job is, in words, for the dispatch-jobs grid
maxRetries, timeoutSeconds the platform’s defaults (maxRetries of an explicit 0 means no retries)
metadata none. At most 50 entries, key at most 100 and value at most 1000 characters. A key starting with fn. is refused; the platform adds fn.address, fn.version and fn.invocation

Metadata is an array of {key, value} on the wire and in storage; the SDKs accept a map and sort it by key.

None of it trusts the function:

Check Refusal
(target, path) is in this version’s declaration NOT_DECLARED (also by the runner before it asks the platform)
the target belongs to the function’s application and client NOT_ALLOWED
the target exists NOT_ALLOWED (not found)
the target’s live version has a webhook endpoint accepting POST at the path NOT_ALLOWED (unavailable)
the connection’s service account is one the function may sign as (its application’s, or its own) DISPATCH_SIGNER_NOT_ALLOWED (403; the function hears NOT_ALLOWED)
fields are valid BAD_REQUEST

A target that is not live is refused at call time, not at publish, so a creator may be deployed before its target. The signer rule is also checked at publish: a version whose declared dispatch names an existing connection signed by an account the function may not sign as is refused with 403 DISPATCH_SIGNER_NOT_ALLOWED and nothing is stored. A connection that does not exist yet passes publish and is checked on every call. A connection that names no account, or an account that does not exist, is not refused here; its delivery is declined with that reason. Platform trouble (unreachable, 5xx) is the only retryable class: answer with a retry, as above. The job’s delivery is signed by the connection’s service account, and the receiving function verifies with its own webhook service account, so those two must be the same account (see Declare endpoints, auth, subscriptions and schedules). The function holds no credential at any point.

Dispatch jobs have no de-duplication. A retry after an ambiguous failure (the call timed out but the job was created) can create a second job. If duplicates matter, make the receiving endpoint idempotent, for example by keying on a value in the payload.

fcdev fn dev checks the call against the declared dispatches with the real shape rules and prints would create job: target=... path=... pool=... connection=... group=... payload=..., returning a fake job id. It does not resolve the pool, the connection or the target. See Develop locally with fcdev fn dev.