# Shipment Cancelling

Use the Shipment Cancelling Function to manage cancellation requests. The function receives the shipment details, and the shipping system can update the status to “Cancelled” when applicable.

:::note[]
Cancellation can be declined if the shipment has already been dispatched or delivered.
:::

## Step By Step Guide

On Salla Partners Portal, click on "+ Add New Function"

![SCR-20251118-jhsx.png](https://api.apidog.com/api/v1/projects/451700/resources/366102/image-preview)

Select the App Function you want to add, which is in this case the Shipment Cancelling Function. After selecting, add a name to the function.

![](https://api.apidog.com/api/v1/projects/451700/resources/366166/image-preview)

### Code Details
      
Salla auto-generates starter code, which you can overwrite with the following implementation. This gives you a clean foundation to build your shipping logic and ensures the function behaves the way your integration needs.


![SCR-20251119-ijhh.png](https://api.apidog.com/api/v1/projects/451700/resources/366167/image-preview)
      
<Tabs>
  <Tab title="Explanation">
<Steps>
  <Step title="Function definition">
    This function is autogenerated by Salla. It runs when Salla triggers Shipment Cancellation, receives the `context` data, and must return a `GenericResponse` object.
    ```js
    export default async (context: Shipments): Promise<Resp> {
    ```
  </Step>

  <Step title="Cancel shipment in shipping company">
    Sends a POST request to the shipping company's API to cancel the shipment. The request includes the shipment data from the event payload.
    ```js
    const response = await fetch("https://{SHIPMENT_COMPANY_BASE_URL}/{CANCEL_SHIPMENT}", {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(context.payload.data)
    });
    ```
  </Step>
  <Step title="Return success response">
    Returns a success response with the shipment data. This is autogenerated by Salla and confirms to Salla that the cancellation was processed.
    ```js
    return Resp.success().setData({});
    ```
  </Step>
</Steps>


  </Tab>
  <Tab title="Full Code">
      
Feel free to copy the below code and paste it *(with some modifications)* to the Salla Partners Portal > App Functions.
      
```js
export default async (context: Shipments): Promise<Resp> {
  console.log('Shipment cancelling event triggered');
  // Cancel shipment in the shipping company
  const response = await fetch("https://{SHIPMENT_COMPANY_BASE_URL}/{CANCEL_SHIPMENT}", {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(context.payload.data)
    });
  
  if(!response.ok) {
    return Resp.error().setMessage("Failed transfer data to Shipment system").setStatus(response.status);
  }

  return Resp.success().setData({});
}; 
```
  </Tab>
</Tabs>

### Sequence Diagram
      
The following diagram shows visually how the shipment cancelled app function works within Salla Partners Portal:
      
```mermaid
sequenceDiagram
    actor m as Salla Merchant
    participant s as App Function: Shipment Cancelling
    participant a as Your Server
        m-->>s: Cancel Shipment
        s-->>a: Cancel shipment in the shipping company via API
        s-->>m: Return shipment details
```

