{
  "name": "ShipPickup: buy the cheapest label",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "shippickup-label",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a1000000-0000-4000-8000-000000000001",
      "name": "Order webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300],
      "webhookId": "shippickup-label"
    },
    {
      "parameters": {
        "jsCode": "// Body of the webhook: order_id, from, to, parcel, items, declared_value_cents (same fields as the ShipPickup API).\nconst order = $input.first().json.body;\nconst id = String(order.order_id).replace(/[^A-Za-z0-9_-]/g, '_');\nconst idempotencyKey = ('n8n-order-' + id).padEnd(16, '0').slice(0, 64);\nconst ratesRequest = { from: order.from, to: order.to, parcel: order.parcel };\nif (order.declared_value_cents !== undefined) ratesRequest.declared_value_cents = order.declared_value_cents;\nconst { order_id, ...shipment } = order;\nreturn [{ json: { ratesRequest, shipment, idempotencyKey, orderId: order_id } }];"
      },
      "id": "a1000000-0000-4000-8000-000000000002",
      "name": "Prepare",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [460, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.shippickup.com/v1/rates",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.ratesRequest) }}",
        "options": {}
      },
      "id": "a1000000-0000-4000-8000-000000000003",
      "name": "Get rates",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [680, 300],
      "credentials": {
        "httpHeaderAuth": {
          "id": "",
          "name": "ShipPickup API key"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// The rates are sorted by price: the first one is the cheapest.\nconst rates = $input.first().json.data;\nif (!rates || rates.length === 0) throw new Error('No rates for this route and parcel');\nconst prepared = $('Prepare').first().json;\nreturn [{ json: { body: { rate_id: rates[0].rate_id, ...prepared.shipment }, idempotencyKey: prepared.idempotencyKey } }];"
      },
      "id": "a1000000-0000-4000-8000-000000000004",
      "name": "Pick cheapest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [900, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.shippickup.com/v1/shipments",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "={{ $json.idempotencyKey }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.body) }}",
        "options": {}
      },
      "id": "a1000000-0000-4000-8000-000000000005",
      "name": "Buy label",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1120, 300],
      "credentials": {
        "httpHeaderAuth": {
          "id": "",
          "name": "ShipPickup API key"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ order_id: $json.data.id, tracking_number: $json.data.tracking_number, status: $json.data.status, carrier: $json.data.carrier, price_cents: $json.data.price_cents }) }}",
        "options": {}
      },
      "id": "a1000000-0000-4000-8000-000000000006",
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1340, 300]
    }
  ],
  "connections": {
    "Order webhook": { "main": [[{ "node": "Prepare", "type": "main", "index": 0 }]] },
    "Prepare": { "main": [[{ "node": "Get rates", "type": "main", "index": 0 }]] },
    "Get rates": { "main": [[{ "node": "Pick cheapest", "type": "main", "index": 0 }]] },
    "Pick cheapest": { "main": [[{ "node": "Buy label", "type": "main", "index": 0 }]] },
    "Buy label": { "main": [[{ "node": "Respond", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "active": false,
  "pinData": {}
}
