Search docs.molo17.com
    Configuration guide Connectors

    Send MySQL changes to a REST API with Courier

    When the system that needs your data is an application with a REST API, not a database, Gluesync can call that API for every change. You describe the API once in Courier, the REST editor of the Control Plane: its base URL, its authentication, and an endpoint with the values it expects. Then a pipeline maps the columns of a source table onto those values. This guide sends the changes of a MySQL table to a CRM API, one PUT per row, and shows what happens when the API answers with an error.

    In this guide

    You will learn how to create a Courier collection and an endpoint with PATH and BODY bindings, send a test request, add the Gluesync Courier agent to a pipeline, create the entity from Courier with Map to pipeline, read the requests the API receives for a snapshot and for inserts, updates and deletes, and see how a failed call is retried with the same idempotency key.

    The steps were run on MySQL 8.4 as the source and a test HTTP API that echoes every request.

    Quick information

    Goal

    Deliver every change of a MySQL table to a REST API as an HTTP request

    Audience

    Data engineers and integration developers

    Level

    Intermediate

    Time required

    About 30 minutes

    Area

    Control Plane → Courier → Outbound; Control Plane → Pipelines

    Applies to

    Gluesync 2.3

    Before you begin

    Make sure that:

    1. A MySQL source is ready for CDC: binary log in ROW format with FULL row images, and a user with REPLICATION SLAVE, REPLICATION CLIENT and SELECT. See Set up MySQL and MariaDB CDC;

    2. The API is reachable from the host that runs Core Hub, on an address that is not loopback, link-local or a cloud metadata service (see the egress rule in HTTP Target);

    3. You have the credentials the API expects, here a bearer token;

    4. You are a Manager or a Super Admin, to create collections in Courier.

    How a row becomes a request

    Part of the request Where it comes from

    URL

    The Base URL of the collection and the Path of the endpoint, with {id} replaced by the value of the PATH binding id

    Headers

    Content-Type, the authentication of the collection, the headers of the endpoint, and an Idempotency-Key that identifies the change

    Body (POST, PUT, PATCH)

    The field operation (Insert, Update or Delete), then one field per BODY binding

    One HTTP method serves every operation: the API tells them apart by the operation field.

    Guide steps

    Optional: create the demo table

    SQL

    Skip this step if you use your own table. On MySQL:

    CREATE TABLE crm.customers (
      customer_id INT PRIMARY KEY,
      name        VARCHAR(60) NOT NULL,
      email       VARCHAR(120),
      tier        VARCHAR(10) NOT NULL,
      active      TINYINT(1) NOT NULL DEFAULT 1
    );
    INSERT INTO crm.customers VALUES
      (1,'Rossi Costruzioni','info@rossi.example','gold',1),
      (2,'Bianchi Srl','sales@bianchi.example','silver',1),
      (3,'Verdi Logistica',NULL,'bronze',0);
    Describe the API in Courier

    UI

    1. In the Control Plane, open Courier. On Outbound, select Create your first collection (or New collection);

    2. Fill in Name (crm, the target schema in pipelines) and Base URL (http://crm-api:8080). In Authentication, choose Bearer token and paste the token. Select Create;

    3. Select New endpoint. Choose the method PUT, type the path /customers/{id} and the Name customers (the target table in pipelines);

    4. The path is marked Placeholders without a PATH binding: id. Select Add PATH binding "id" and set its Type to Number;

    5. Select Add binding for each value of the body, with Location Body: name (String, required), email (String, not required), tier (String, required) and active (Boolean, required).

    Courier endpoint customers with PUT /customers/{id}
    Figure 1. The endpoint and its bindings. Request preview shows the call with the sample values.
    1. Select Send test request. It sends the unsaved definition with the sample values;

    2. Select Save.

    Courier endpoint with the test result: Status 200
    Figure 2. The API answered 200. A credential the API echoes back is shown as ***.
    Expected result

    Status 200 and the URL http://crm-api:8080/customers/1. The API received PUT /customers/1 with the body {"operation":"Update","name":"Rossi Costruzioni","email":"info@rossi.example","tier":"gold","active":true}.

    An endpoint without a required PATH binding gets the synthetic key column _row, and the editor says so: No required PATH binding. Give the endpoint a PATH binding fed by the source key, so that each row goes to its own resource.

    Create the pipeline

    UI

    1. Select Create pipeline and choose the MySQL CDC agent. Fill in the connection and select Test and continue;

    2. In Select target agent, search for Courier and select Gluesync Courier. Searching for HTTP or REST finds nothing: the agent is listed under this name;

    3. In Outbound collection, choose crm. Base URL and Authentication are read from the collection;

    4. Select Test and continue, name the pipeline (CustomersToCRM) and select Finish.

    Gluesync Courier agent setup with Outbound collection crm
    Figure 3. The Gluesync Courier agent takes everything from the Courier collection.

    On MySQL 8.4, where users authenticate with caching_sha2_password, a connection without TLS needs Allow public key retrieval on the MySQL connection. Without it, the test failed with Failed to initialize pool: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

    Create the entity from Courier

    UI

    The entities of a pipeline with the Courier agent are created from the endpoint. Entities on the pipeline page says so and offers Open Courier.

    Notice Create this entity from Courier: this pipeline writes to an HTTP Target
    Figure 4. Create this entity from Courier: the entity is built from the endpoint side.
    1. In Courier, select the endpoint customers, then Map to pipeline;

    2. Pipeline, HTTP Target agent and Source agent are filled in. Choose Source schema crm and Source table customers;

    3. Under Column → binding mapping, the bindings with the same name as a column are mapped. Map id to customer_id (PK);

    4. Select Save entity.

    Map to pipeline dialog: Deliver a source table to crm / customers
    Figure 5. Every binding has a source column. Request sent for each change shows the call.
    Expected result

    The dialog shows Already delivered: the entity customers_to_customers writes to this endpoint.

    While id is not mapped, Save entity stays disabled and the dialog lists Binding "id" is required and has no default value: map a source column to it. and Primary key column "customer_id" must be mapped to a binding.

    Start the entity and read the requests

    UI + SQL

    1. Open the entity on the pipeline page, select Start, keep Full copy and Keep and merge, and select Start entity;

    2. Check the requests your API received;

    3. On MySQL, insert, update and delete a row, and check the requests again:

      INSERT INTO crm.customers VALUES (4,'Neri Consulting','hello@neri.example','silver',1);
      UPDATE crm.customers SET tier = 'platinum' WHERE customer_id = 1;
      DELETE FROM crm.customers WHERE customer_id = 2;
    Expected result

    One request per row of the snapshot, then one per change. Each carries its own Idempotency-Key header:

    PUT /customers/1  {"operation":"Update","name":"Rossi Costruzioni","email":"info@rossi.example","tier":"gold","active":"1"}
    PUT /customers/2  {"operation":"Update","name":"Bianchi Srl",...,"active":"1"}
    PUT /customers/3  {"operation":"Update","name":"Verdi Logistica","email":null,"tier":"bronze","active":"0"}
    PUT /customers/4  {"operation":"Insert","name":"Neri Consulting",...}
    PUT /customers/1  {"operation":"Update",...,"tier":"platinum",...}
    PUT /customers/2  {"operation":"Delete","name":"Bianchi Srl",...}

    The snapshot rows carry Update, because a snapshot row is an upsert. A NULL column is sent as null. The delete goes to the URL of the row and carries its last values.

    Known limitation: a MySQL TINYINT(1) column such as active reaches a Boolean binding as the string "1" or "0", not as true or false. Until this is fixed, make the API accept both forms.

    See a failed call retried

    UI + SQL

    A call that does not get a 2xx answer stops the entity, and Core Hub tries the change again from its checkpoint. To see it, make any API you can control return 503 for a while. The test used the open-source echo server mendhak/http-https-echo, which answers with the status given in the request header x-set-response-status-code:

    1. In Courier, on the endpoint, open Headers, select Add header, and add x-set-response-status-code with the value 503. Select Save. With another API, make it answer 503 in its own way;

    2. On MySQL, update a row: UPDATE crm.customers SET email = 'team@verdi.example' WHERE customer_id = 3;;

    3. Open the entity.

    Entity customers_to_customers in Error with Target write failed: HTTP 503 from PUT http://crm-api:8080/customers/3
    Figure 6. Target write failed gives the status, the method, the URL and the start of the answer, with credentials masked.
    1. Remove the header in Courier and select Save;

    2. Wait for the entity.

    Expected result

    While the API answers 503, the entity is in Error. In the test, Core Hub sent the same change six times, at growing intervals (3, 4, 8, 16 and 32 seconds), and the sixth call got 200. The entity went back to Active by itself.

    Every attempt carried the same Idempotency-Key. An API that remembers the keys it has seen can answer a repeat without applying the change twice.

    Verify the result

    You send MySQL changes to the REST API if:

    • Send test request on the endpoint answered 2xx;

    • the snapshot sent one request per row, with "operation":"Update";

    • an insert, an update and a delete reached the API with Insert, Update and Delete;

    • after a refused call, the entity recovered by itself once the API accepted the change again.

    Troubleshooting

    Problem Possible cause What to do

    Outbound call to localhost refused: loopback/link-local/metadata/Core Hub addresses are blocked; set GS_COURIER_ALLOW_LOCAL_EGRESS=true to allow them

    The base URL resolves to a loopback, link-local or metadata address, or to Core Hub itself

    Use an address the Core Hub host reaches on your network. Set GS_COURIER_ALLOW_LOCAL_EGRESS=true on Core Hub only for a test API on the same host

    Target write failed: HTTP <status> from <method> <url>: …​, entity in Error

    The API refused the call

    Read the status and the answer in the error, fix the API side, and wait: Core Hub retries the change

    Entities on the pipeline page shows Create this entity from Courier

    A Courier pipeline creates entities from the endpoint

    Select Open Courier, then Map to pipeline on the endpoint

    Save entity is disabled in Map to pipeline

    A required binding has no source column, or the source key is not mapped

    Map the source primary key onto the PATH binding, and every required binding onto a column

    Placeholders without a PATH binding: id

    The path uses a placeholder that has no binding

    Select Add PATH binding "id"

    No agent found when you search for HTTP in the target list

    The agent is listed as Gluesync Courier

    Search for Courier

    A Boolean binding receives "1" or "0"

    The source column is a MySQL TINYINT(1)

    Accept both forms in the API