<@ULVA73B9P> How to send a custom event to self-ho...
# ask-marvin
l
@Marvin How to send a custom event to self-hosted prefect server? I want to create an automation and trigger a flow run when this event is sent.
m
thought for 326.2 seconds
n
marvin should say something along the lines of "use `emit_event`"!
m
Great question — you can absolutely trigger a flow run from a custom event in a self-hosted Prefect 3 Server. Here’s the end-to-end approach. Assumptions - You’re on Prefect 3.x and running a self-hosted Server. - Your Server is reachable at something like http//&lt;your host&gt;4200 and you’ve set PREFECT_API_URL accordingly. Step 1) Configure auth (self-hosted Server) - If you’ve enabled API auth on your Server, set a Basic Auth string on the Server process: - Config key:
server.api.auth_string
- Env var:
PREFECT_API_AUTH_STRING
(e.g.,
user:pass
) - Use that same
user:pass
in your client calls. - Docs: Security settings Step 2) Create an Automation to run your deployment on event - In the Prefect UI: - Go to Automations → Create - Trigger: Event - Condition: Event name equals (for example)
acme.order.created
- Action: Run Deployment (choose the deployment you want to start) - You can add filters on resources if you want to target specific IDs/tags. Step 3) Send a custom event Option A: From Python using Prefect’s emitter - This uses your
PREFECT_API_URL
(e.g.,
http://<your-host>:4200/api
) and
PREFECT_API_AUTH_STRING
if set.
Copy code
import os
from prefect.events import emit_event

# Point at your self-hosted Server
os.environ["PREFECT_API_URL"] = "http://<your-host>:4200/api"
# Only if you enabled auth on the server:
os.environ["PREFECT_API_AUTH_STRING"] = "user:pass"

emit_event(
    event="acme.order.created",
    resource={
        "prefect.resource.id": "acme/order/12345",
        "order_id": "12345"  # optional extra labels
    },
    payload={
        "amount": 42.50,
        "currency": "USD"
    },
)
- Python API reference: emit_event Option B: Direct HTTP POST (curl) - Endpoint:
POST /api/events
- Auth: Basic auth if enabled (same
user:pass
as
PREFECT_API_AUTH_STRING
) - Body: An array of event objects Minimal example:
Copy code
curl -X POST "http://<your-host>:4200/api/events" \
  -u "user:pass" \
  -H "Content-Type: application/json" \
  -d '[
    {
      "event": "acme.order.created",
      "resource": {
        "prefect.resource.id": "acme/order/12345",
        "order_id": "12345"
      },
      "payload": {
        "amount": 42.50,
        "currency": "USD"
      }
    }
  ]'
- API reference: Create events Event payload schema (what the server accepts) - The endpoint accepts a list of events. Each event supports: -
event
(string): your event name, e.g.,
acme.order.created
-
resource
(object): a string-to-string map; include at least
prefect.resource.id
-
related
(array of objects): optional; same shape as
resource
items -
occurred
(ISO timestamp): optional; if omitted, server will use receive time -
payload
(object): optional; any extra JSON you want to attach -
id
, `follows`: optional advanced fields Useful tools while testing - Stream events live to confirm your custom event is received:
Copy code
prefect events stream --format text
Notes and differences from Prefect 2.x - There is no CLI command like
prefect events send
in Prefect 3.x (the CLI supports streaming, not sending). - In 3.x, the event system and automations are first-class in self-hosted Server as well as Cloud. Would you like me to sketch the exact automation trigger and action config for your deployment name and parameters? If you share your Server URL pattern and whether you’ve enabled auth, I can tailor the code/curl snippets precisely.