Cargo-Planner Docs

Two way communication

A frequent usage pattern is that you send some input data to our API, we do the number crunching and you will then be presented with a result, which you can embed into your application.

Sometimes you want to manually do some rearrangements where you might want to delete some cargoes from a container, and and some others. When you have made those kind of modifications it would be great if your application got those changes as well - you want Cargo-Planner to interact with your system as well. You’ll then want to add some webhooks.

Webhooks

Webhooks are basically endpoints you set to to subscribe to certain events.

Lets say that as soon as a loadplan is saved / updated by you, you want this result to be sent to your server. This is how you achieve that:

Creating a webhook

  1. Go to the Developer portal in the web application
  2. Expand the webhooks section
  3. Press create webhook
  4. Enter the URL you want the data to be posted to as well as selecting which event you want to subscribe to
  5. Press save

Listening to events

On your server, add an endpoint, same as the one you specified in the URL section above when creating the endpoint


from flask import Flask, request, abort
import hmac
import hashlib

app = Flask(__name__)

@app.route('/events-from-cargoplanner/', methods=['POST'])
def receive_webhook():
    payload = request.get_data(as_text=True)
    received_signature = request.headers.get('X-Hub-Signature')

    secret = "_nf7TmrgTJt6Gk43d9aSAK_yOEE58KXlTgvotei3Xe4"  # Add the secret generated for your endpoint

    expected_signature = hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()

    if hmac.compare_digest(f'sha256={expected_signature}', received_signature):
        # Process the payload
        return "Payload processed", 200s
    else:
        abort(403, 'Signature verification failed')

if __name__ == '__main__':
    app.run()

When creating a webhook you will receive a secret which can be used on your server to make sure that the data actually comes from Cargo-Planner