Skip to content
Recipes

API to API

A signed JSON webhook in, checked against a JSON Schema, mapped per sender, posted to a REST API with OAuth2, and answered with the API's response. Plus the polling variant for an API that can't push.

On this page

Each Demo Health clinic posts new appointments to MedHook as JSON. MedHook checks the body, maps it to the scheduling API's JSON with that clinic's settings, posts it with an OAuth2 token, and answers the clinic with the scheduling API's confirmation. A failed call is answered at once, and you replay it from the engine when the API is back.

Source and destination

sequenceDiagram
    participant Clinic as Demo Health clinic
    participant Engine as MedHook engine
    participant Token as Token endpoint
    participant API as Scheduling API

    Clinic->>Engine: POST /api/triggers/webhook/demo-health-appointments (signed JSON)
    Engine->>Engine: Signature, timestamp, JSON Schema
    Engine->>Engine: Data map with the sender's settings
    Engine->>Token: client_credentials (once, then cached)
    Token-->>Engine: access_token
    Engine->>API: POST /accounts/{accountId}/appointments
    API-->>Engine: 201 { id, status }
    Engine-->>Clinic: 201 { confirmation, status }
  • Source: a webhook trigger with a signing secret, a body schema, senderFrom and response.mode destination.
  • Destination: one Generic REST step, action post, with OAuth2 client credentials.
  • Per sender: the workflow's sender table holds each clinic's accountId and default clinicCode.

What it uses

Status

Available on this release. An automated test runs this recipe's workflow file against a local token endpoint and a local scheduling API on every change.

The workflow file

Import the workflow file under Workflows, then Import › Import JSON. It holds the trigger, the three steps and their settings. It has no credentials: you add the signing secret and choose the adapter profile in the steps below.

The body the clinics send:

json
{
  "sender": "dh-north",
  "appointmentId": "APT-1001",
  "start": "2026-11-02T09:30:00Z",
  "durationMinutes": 20,
  "reminder": "sms",
  "patient": { "mrn": "TESTMRN0001", "family": "TESTPATIENT", "given": "ALEX" }
}

Steps

  1. Under Adapters, create a Generic REST profile named Scheduling API: base URL https://scheduling.example.test/api/v1, authentication oauth2, with the client id, client secret and token URL (https://scheduling.example.test/oauth/token) the API owner gave you. The engine fetches a token with the client credentials grant, caches it until it expires, and fetches a new one once if the API answers 401.

    Auth0, Okta and Azure AD often want more: set OAuth2 audience (oauth2Audience) to the API's identifier, OAuth2 client auth to client_secret_post when the token endpoint reads the secret from the form, or choose oauth2-private-key-jwt and give the profile a private key instead of a secret. See OAuth2.

  2. Import the workflow file. On Post to the scheduling API, choose Scheduling API. Its endpoint is /accounts/{{sender.accountId}}/appointments.

  3. On the trigger, keep Webhook id demo-health-appointments or choose your own, and set a signing secret through the API ("secret": "whsec_..."). Signed timestamp is required, so a captured request can't be sent again later.

  4. Choose Settings, and under Delivery, in Senders, write one line per clinic. senderFrom reads the sender field of the body, so these keys are the values clinics send:

    text
    dh-north: accountId=acct-north; clinicCode=NORTH-01
    dh-south: accountId=acct-south; clinicCode=SOUTH-01

    Choose Save senders.

  5. Deploy the workflow.

  6. Give each clinic the URL https://engine.example.test/api/triggers/webhook/demo-health-appointments and the signing secret. They sign with Standard Webhooks headers (webhook-id, webhook-timestamp, webhook-signature), which most webhook libraries send.

What the steps do

Map to the scheduling API is a data map from the clinic's JSON to the API's JSON:

SourceTargetNotes
$.data.appointmentIdexternalId
$.data.patient.mrnpatient.identifier
$.data.patient.familypatient.lastName
$.data.patient.givenpatient.firstName
$.data.startslot.start
$.data.durationMinutesslot.minutes30 when the clinic leaves it out
$.data.clinicCodeslot.clinicThe sender's clinicCode when the body has none
$.data.reminderreminderChannelsms becomes TEXT, email becomes EMAIL, anything NONE

Post to the scheduling API posts the map's output as JSON. The engine adds an Idempotency-Key header that stays the same when it sends the same step for the same message again, so an API that honours it drops a resend.

To change an appointment the API already has, send to its own URL: a Generic REST step with action put (or patch, delete) and "urlTemplate": "/appointments/{{data.id}}" in place of endpoint. The id comes from the step's input, encoded as one path segment, and can't change the host. See URL templates.

When the API answers 201 for a booking and 202 for one it queued, set the post step's response to full. Its output is then { "status", "headers", "body" }, and a condition step after it branches on data.status === 201; the Location header is data.headers.location. See The response.

Answer the clinic maps the API's response, { "id": "CONF-...", "status": "booked" }, to { "confirmation": "CONF-...", "status": "booked" }. The trigger answers the clinic with this step's output and status 201, once the run ends.

Test it

  • A signed body from dh-north gets 201 { "confirmation": "CONF-...", "status": "booked" }. The scheduling API receives POST /api/v1/accounts/acct-north/appointments with Authorization: Bearer <token> and slot.clinic NORTH-01.
  • A body from dh-south with "clinicCode": "SOUTH-07" goes to acct-south with slot.clinic SOUTH-07. The token endpoint is called once for both.
  • A body without patient.mrn and with "start": "next tuesday" gets 400 BODY_SCHEMA_INVALID with "/patient/mrn: is required" and "/start: must be a date-time (RFC 3339)". Nothing is sent.
  • An unsigned body gets 403. A signed body with a timestamp from 15 minutes ago gets 401 TIMESTAMP_OUT_OF_TOLERANCE.
  • In Messages, open a run and choose Delivered: the host, POST, status 201, the request and response bodies, and the Authorization header shown as [redacted]. Its Correlation id is the clinic's x-request-id, which the scheduling API received as X-Request-Id; a clinic that sends none gets the engine's id back in the response's X-Request-ID header.

When the API fails

When the scheduling API answers 503, the run fails and the clinic gets 502 with the execution id, never the error text. The workflow file sets Workflow retries to 0, so the clinic decides whether to resend. Raise it to have the engine retry first: the clinic then gets the answer of the last attempt, within the trigger's 20-second wait.

The failed run is on Messages › Needs action. Once the API is back, choose Replay on it, or call POST /api/executions/{id}/replay. The replay runs the same message again, with the same sender settings, and the API receives it once.

The polling variant

When the source can't push, poll it. A polling trigger on a Generic REST profile reads the API on a schedule, follows its pages, and runs the workflow once per new item. It keeps a cursor in PostgreSQL between polls, so each poll reads only what changed, across restarts and replicas.

json
{
  "type": "polling",
  "interval": 60000,
  "adapterProfileId": "<the source API's profile>",
  "action": "get",
  "params": {
    "urlTemplate": "/appointments?updatedSince={{cursor}}",
    "pagination": {
      "mode": "next-token",
      "itemsPath": "$.data",
      "nextPath": "$.nextPageToken",
      "param": "pageToken"
    }
  },
  "idField": "id",
  "cursorField": "updatedAt",
  "startFrom": "beginning"
}

Each item then runs the same kind of steps: a data map, or a JavaScript step that builds an HL7v2 message for an MLLP send, or a BigQuery insert. The page's itemsPath sits inside pagination: the trigger's own Items path stays empty. See Poll a paginated REST API for the setup, Polling for the cursor rules, Pull from an API to HL7v2 over MLLP for the HL7v2 destination, and FHIR Bulk to a warehouse for BigQuery.

Reference