Connect Snipcard to Sendcloud API on order.completed event

hi snipcart support team,

i need support connecting snipcart to sendcloud. what i wanna do is use the order.completed event to call the sendcloud api and create a new shipment/parcel there (i think this is the endpoint to use but not 100% sure: https://docs.sendcloud.sc/api/v2/shipping/?shell#create-a-parcel). can you point my to an implmentation, where i can see how to use snipcart’s webhooks or where to put the relevant code to be executed, when the event order.completed is triggered.

next question would be how to safely use my sendclound api-key and api-secret in javascript when communicating with the api.

thanks for your help
correa

Hi @correa, thanks for reaching out!

For that you would need to use a backend or a serverless function.
You need to create an endpoint to receive the order data, and with that data send a request to the Sendcloud API. Also, doing it that way, your API key and secret are not exposed in the JS of the page.

You can read more about our webhooks here.

Let me know if you have further questions.

Thanks.

hi @nelitow,

thanks for your answer. figured that part out already and created the endpoint. (the link regarding webhooks, could definetely need some enhancements on how to implement them)

can you point me somewhere, where it is explained how to explore the “order item” which is passed to the endpoint. ideally, this would also include some guiding on how to transform the order json into a php/curl request which i can send to the sendcloud api.

thanks for your support
correa

Deleted my reply since the order.completed Event wasn’t accessible in the client frontend as i tried to implement it. (Probably bc it’s a webhook :slight_smile: )
Instead, what worked for me, was creating a php file on my server. In the snipcart backend webhooks tabs (Login - Snipcart), i had to point snipcart to my php file which works as the endpoint. There i put the php from the documentation: Webhooks: examples – Snipcart Documentation.
In the php, i am accessing my api keys and secret which i store in a .htaccess file (Not sure how secure that is in the end).
Afterward, when i completed a snipcart order, i could see in the snipcart backend, that my php endpoint was used .

That’s great @correa!

Is there anything else we can help you with?

Thanks.

@nelitow thanks for your quick answer.

yes. i need to figure out how to get all the necessary information out of the order json, (order payload) and “convert” it into a working request, which i can send to the sendcloud api. can you point me somewhere, where sth similar is done in php?

:pray:

Hi @correa.

We don’t have a precise example for that, but the idea is that you capture the webhook data, build an array with the information needed, then send the request to Sendcloud’s API.
If you’re using basic PHP, it would be something like this:

<?php
$data = json_decode(file_get_contents('php://input'), true);

$name = ($data["content"]["billingAddressName"]);

// build api data
$api_user = 'api_user';
$api_key = 'api_key';
$param = array(
    'parcel' => array(
        'name' => $name,
        'description' => '',
        'sender' => array(
            'name' => '',
            'address1' => '',
            'address2' => '',
            'city' => '',
            'state' => '',
            'postal_code' => '',
            'country' => '',
            'phone' => '',
            'email' => '',
            'company' => '',
            'tax_id' => '',
            'vat_id' => '',
        ),
        'weight' => '0.5',
    )
);

// send post request to sendcloud
$url = 'https://panel.sendcloud.sc/api/v2/parcels';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Basic ' . base64_encode($api_user . ':' . $api_key)
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

Please note that this is untested code created only for your reference. In this code I only get the name as an example, you would need to get all relevant data required.
To test this, you can create a fake request in Postman and simulate a webhook call so that you can check the response more easily than creating a test order each time.

Here I am using var_dump($param); to check the data received over POST in the PHP file:

I hope this sheds some light into how you can get that working.

Thanks!

1 Like

hi @nelitow, thanks for the guiding, will work through your code examples and get back to you asap.

1 Like

@nelitow

thanks again, got it basically working with your support and code hints.

need to figure out some problems with the ordering of street and house number now, bc the sendcloud’s api doesn’t eat it like that. found street / house number topic, probably, the flag which is mentioned there will help.

@nelitow based on snipcart’s info here, i would like to secure the communication with sendcloud’s api and need support to implement the necessary steps

sendcloud offers these information regarding their api/webhooks and this link regarding payload, validation.

can you help me connect the dots? do i need to contact sendcloud’s api twice, first for “handshake”, if valid, then to create parcel? do i do this in the above mentioned php-file?