Example Integration with FedEx
In this tutorial, we will show how to integrate FedEx.
1. Preliminary Preparation
1.1. Create a FedEx Account
Create an account on the developer portal.
1.2. Create Projects and Obtain Tokens
Create 2 projects in the my projects section and save the client id and client secret.
The first project is for managing shipments.
The second project is for tracking shipments.
1.3. Create Delivery Statuses
In the order storage status tab, create statuses:
- order_delivery
- order_delivery_started
- ready_for_pickup
1.4. Create an Event for Tracking Statuses
In the Events module, create an event to track the order status change order_delivery
.
1.5. Create a Collection for Delivery
In the Integration Collections module, create a collection for delivery with the following attributes:
- name (
string
) - address (
string
) - phone_number (
string
) - city (
string
) - state_code (
string
) - postal_code (
string
) - country_code (
string
) - tracking_number (
string
) - transaction_id (
string
) - ship_datestamp (
string
) - delivery_datestamp (
string
)
1.6. Create a Test Order
Create an order and a record in the collection for this order.
2. Creating a Shipment
2.1. Tracking the Delivery Event
- Drag the events node onto the workspace.
- Enter the name of the node.
- Change the value in the "Set" field to "orderEvent".
- The event will be saved in the "msg.orderEvent" object and can be used in other nodes.
- Select the created event "order delivery" from the list and click "submit".
2.2. Obtaining a Token
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
- Replace
YOUR_CLIENT_ID
andYOUR_CLIENT_SECRET
with your own.
msg.payload = {
'grant_type': 'client_credentials',
'client_id': 'YOUR_CLIENT_ID',
'client_secret': 'YOUR_CLIENT_SECRET'
}
msg.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
return msg;
More about the parameters can be found in the FedEx documentation.
- Drag the http request node onto the workspace and connect it to the node from the previous step.
- Select "POST" in the Method field.
- Select "Send as request body" in the Data field.
- Paste the link
https://apis-sandbox.fedex.com/oauth/token
in the Link field. - Select "Object JSON" in the Return field.
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
msg.access_token = msg.payload.access_token;
return msg;
2.3. Creating a Shipment
- Drag the api node onto the workspace and connect it to the node from the previous step.
- Select "Find collection rows" as the method and paste the code from the description below into the "Data" field.
{
"collectionIdentifier": "delivery",
"entityId": "{{orderEvent.order.id}}",
"entityType": "orders",
"langCode": "en_US",
"limit": 1,
"offset": 0
}
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
// get the previously saved token
const token = msg.access_token;
// set the headers
msg.headers = {
'Content-Type': 'application/json',
'authorization': `Bearer ${token}`
};
// get collections from the previous step
const collection = msg.payload.items[0];
// convert the array to an object
const formData = collection.formData.reduce((acc, item) => {
acc[item.marker] = item.value;
return acc;
}, {});
// set recipient parameters from the form
const recipient = {
"address": {
"streetLines": [
formData.address
],
"city": formData.city,
"stateOrProvinceCode": formData.state_code,
"postalCode": formData.postal_code,
"countryCode": formData.country_code
},
"contact": {
"personName": formData.name,
"phoneNumber": formData.phone_number
}
}
// set collection data in msg for future use
msg.collection = collection;
// set request parameters for FedEx
msg.payload = {
"labelResponseOptions": "URL_ONLY",
"accountNumber": {
"value": "510087020"
},
"requestedShipment": {
"shipper": {
"contact": {
"personName": "SENDER NAME",
"phoneNumber": "9018328595"
},
"address": {
"streetLines": [
"SENDER ADDRESS 1"
],
"city": "MEMPHIS",
"stateOrProvinceCode": "TN",
"postalCode": "38116",
"countryCode": "US"
}
},
"recipients": [
recipient
],
"serviceType": "STANDARD_OVERNIGHT",
"packagingType": "YOUR_PACKAGING",
"pickupType": "DROPOFF_AT_FEDEX_LOCATION",
"shippingChargesPayment": {
"paymentType": "SENDER",
"payor": {
"responsibleParty": {
"accountNumber": {
"value": "510087020",
"key": ""
}
},
"address": {
"streetLines": [
"SENDER ADDRESS 1"
],
"city": "MEMPHIS",
"stateOrProvinceCode": "TN",
"postalCode": "38116",
"countryCode": "US"
}
}
},
"labelSpecification": {},
"requestedPackageLineItems": [
{
"customerReferences": [
{
"customerReferenceType": "CUSTOMER_REFERENCE",
"value": `Collection${collection.id}`
}
],
"weight": {
"units": "LB",
"value": "20"
}
}
]
}
};
return msg;
More about the parameters can be found in the FedEx documentation.
- Drag the http request node onto the workspace and connect it to the node from the previous step.
- Select "POST" in the Method field.
- Select "Send as request body" in the Data field.
- Paste the link
https://apis-sandbox.fedex.com/ship/v1/shipments
in the Link field. - Select "Object JSON" in the Return field.
- Drag the debug node onto the workspace and connect it to the node from the previous step.
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
// get shipment data
const shipment = msg.payload.output.transactionShipments[0];
// get collection data saved earlier
const collection = msg.collection;
// write shipment data into form attributes
const newFormData = [
...collection.formData
.filter(item => !['transaction_id', 'ship_datestamp', 'delivery_datestamp', 'tracking_number'].includes(item.marker)),
{
marker: 'transaction_id',
value: msg.payload.transactionId,
type: 'string'
},
{
marker: 'ship_datestamp',
value: shipment.shipDatestamp,
type: 'string'
},
{
marker: 'delivery_datestamp',
value: shipment.pieceResponses[0].deliveryDatestamp,
type: 'string'
},
{
marker: 'tracking_number',
value: shipment.masterTrackingNumber,
type: 'string'
}
]
// set parameters for updating the collection
msg.payload = {
id: collection.id,
collectionIdentifier: 'delivery',
formData: {
en_US: newFormData
},
formIdentifier: 'delivery_form',
entityId: collection.entityId,
entityType: collection.entityType,
langCode: 'en_US',
}
// save collection data for further use
msg.collection = msg.payload;
return msg;
- Drag the api node onto the workspace and connect it to the node from the previous step.
- Select "Update collection row" as the method.
2.4. Updating the Order Status
- Drag the api node onto the workspace and connect it to the node from the previous step.
- Select "Update order status" as the method and paste the code from the description below into the "Data" field.
{
"orderId": "{{collection.entityId}}",
"storageId": "test_order_storage",
"statusIdentifier": "order_delivery_started"
}
- Drag the debug node onto the workspace and connect it to the node from the previous step.
3. Tracking Shipment Status
3.1. Obtaining Orders for Tracking
- Drag the interval node onto the workspace and connect it to the node from the previous step.
- Enter the interval in minutes.
- Drag the api node onto the workspace and connect it to the node from the previous step.
- Select "Find orders by status" as the method and paste the code from the description below into the "Data" field.
{
"storageId": "test_order_storage",
"statusIdentifier": "order_delivery_started"
}
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
msg.payload = msg.payload.items;
return msg;
- Drag the split node onto the workspace and connect it to the node from the previous step.
- Enter the length.
- Drag the api node onto the workspace and connect it to the node from the previous step.
- Select "Find collection row" as the method and paste the code from the description below into the "Data" field.
{
"collectionIdentifier": "delivery",
"entityId": "{{payload.id}}",
"entityType": "orders",
"langCode": "en_US",
"limit": 1,
"offset": 0
}
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
msg.payload = msg.payload.items[0]
return msg;
- Drag the switch node onto the workspace and connect it to the node from the previous step.
- Add a rule for checking. Select the condition "not empty".
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
msg.trackingNumber = msg.payload
.formData
.find(item => item.marker === 'tracking_number')
.value;
msg.collection = msg.payload;
return msg;
3.2. Obtaining a Token
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
- Replace
YOUR_CLIENT_ID
andYOUR_CLIENT_SECRET
with your own.
msg.payload = {
'grant_type': 'client_credentials',
'client_id': 'YOUR_CLIENT_ID',
'client_secret': 'YOUR_CLIENT_SECRET'
}
msg.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
return msg;
More about the parameters can be found in the FedEx documentation.
- Drag the http request node onto the workspace and connect it to the node from the previous step.
- Select "POST" in the Method field.
- Select "Send as request body" in the Data field.
- Paste the link
https://apis-sandbox.fedex.com/oauth/token
in the Link field. - Select "Object JSON" in the Return field.
3.3. Obtaining Shipment Status
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
const token = msg.payload.access_token;
msg.headers = {
'content-type': 'application/json',
'authorization': `Bearer ${token}`
};
msg.payload = {
"trackingInfo": [
{
"trackingNumberInfo": {
"trackingNumber": msg.trackingNumber
}
}
],
"includeDetailedScans": true
}
return msg;
More about the parameters can be found in the FedEx documentation.
- Drag the http request node onto the workspace and connect it to the node from the previous step.
- Select "POST" in the Method field.
- Select "Send as request body" in the Data field.
- Paste the link
https://apis-sandbox.fedex.com/track/v1/trackingnumbers
in the Link field. - Select "Object JSON" in the Return field.
- Drag the function node onto the workspace and connect it to the node from the previous step.
- Paste the code from the example into the "Function" field.
msg.payload = msg.payload.output.completeTrackResults[0].trackResults[0].latestStatusDetail.statusByLocale;
return msg;
3.4. Updating the Order Status
- Drag the switch node onto the workspace and connect it to the node from the previous step.
- Add a rule for checking. Select the condition "==", and enter "Ready for pickup" as the value.
- Drag the api node onto the workspace and connect it to the node from the previous step.
- Select "Update order status" as the method and paste the code from the description below into the "Data" field.
{
"orderId": "{{collection.entityId}}",
"storageId": "test_order_storage",
"statusIdentifier": "ready_for_pickup"
}
- Drag the debug node onto the workspace and connect it to the node from the previous step.