Skip to main content
Version: v2.0

Handle Callbacks in Godspeed

Handling callbacks in your application just requires setting up an HTTP endpoint.

This endpoint will act as a listener for responses from third-party services, which send data or notifications to your server when specific events occur.

Steps to Handle Callbacks in Godspeed

1. Define the Callback URL Endpoint

To receive a callback, define an HTTP endpoint in Godspeed that the third-party service can use as the callback URL.

  1. Create a New Event in the src/events directory. This event will listen for incoming callback requests.

Example callback-event.yaml:

 http.post./callback:
summary: "Handle callbacks from external services"
description: "Endpoint to handle callback responses from external services."
fn: handleCallback
authn: false
body:
content:
application/json:
schema:
type: object
properties:
status:
type: string
data:
type: object
required: [status]
responses:
200:
description: Callback handled successfully
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: Callback received and processed
400:
description: Invalid callback request

Explanation:

  • Path: The callback endpoint listens at POST /callback.
  • Body: Defines the structure of the callback payload (e.g., status and data fields).
  • Responses: Specifies possible responses, such as 200 for successful processing and 400 for invalid requests.

2. Create the Workflow to Process the Callback

Define a workflow to process the callback data, handling any necessary business logic.

  1. Create a Workflow File in the src/functions directory to define how the callback data is processed.

Example handleCallback.yaml:

id: handleCallback
summary: Process callback data from external service
tasks:
- id: validate_callback
fn: validateCallbackData
args:
data: <% inputs.body.data %>
status: <% inputs.body.status %>

- id: process_data
fn: processData
args:
data: <% validate_callback.result %>

- id: respond_success
args:
statusCode: 200
body:
message: Callback received and processed successfully

Explanation:

  • validate_callback: A task to validate incoming callback data.
  • process_data: Processes the data after validation.
  • respond_success: Responds with a 200 status code if all tasks are completed successfully.

3. Set the Callback URL with the Third-Party Service

  1. Register the Callback URL with the external service.

    • Provide the callback URL in the following format: http://<BASE_URL>:<PORT>/callback
    • Replace <BASE_URL> and <PORT> with your actual server's URL and port.

    For example:

    http://localhost:3000/callback
  2. Test the Callback:

    • Use tools like Postman or a similar HTTP client to simulate a POST request to the /callback endpoint.
    • Ensure the payload matches the expected schema to confirm the callback flow.

Example Scenario: Handling Payment Gateway Callback

Let’s say you’re integrating with a payment gateway, and you want to handle a payment status update callback:

  1. Set Up Event and Workflow: Follow the same steps as above to create the event (payment_callback.yaml) and workflow (handlePaymentCallback) files.
  2. Register the Callback URL: Provide the payment gateway with your callback URL.
  3. Process the Callback: In the handlePaymentCallback workflow, define specific tasks to validate payment status, log transactions, or update your database based on the callback data.

This setup gives you full control over handling incoming callbacks, allowing you to process and store data as required. Each component—from the event to the functions—can be customized to suit the specific needs of the callback use case.