Simplest Approach of the FulfillmentService needed

Hello,

we are forced to update our custom app with the FulfillmentService.
What we do right now is requesting a lable and a shipping Id from our shiiping provider and than fulfill the order like this:

function postFullflillment(orderID, trackingNum, shippingComp) {
var ffData = {
“fulfillment”: {
“location_id”: location_id,
“tracking_number”: trackingNum,
“tracking_company”: shippingComp,
“notify_customer”: true
}
};

var headers = {
‘X-Shopify-Access-Token’: token
};

var options = {
“method” : “POST”,
“muteHttpExceptions”: true,
“contentType”: “application/json”,
“headers” : headers,
“payload”: JSON.stringify(ffData)
};

var response = UrlFetchApp.fetch(store + apiPath + ‘orders/’ + orderID + ‘/fulfillments.json’, options);
}

How can we migrate it to the new FulfillmentService? All the Guides and the Doc seem to be much more complicated and including much more components than our current solution. If someone has a code snippet for me I would be very grateful.

The new API allows for advanced use cases where an order can have multiple fulfillments. Minimally, you will need to:

  1. Query the list of fulfillment orders on the order: https://shopify.dev/docs/apps/fulfillment/order-management-apps/manage-fulfillments#step-1-retrieve-an-order

  2. Create a fulfillment for the fulfillment orders. If your use case remains simple, there will only be one. https://shopify.dev/docs/apps/fulfillment/order-management-apps/manage-fulfillments#create-a-fulfillment

Hey RobZone,

thanks for the help!
The first request works fine but with the second one I got the following error
Exception: Request failed for https://test-store-better-by-less.myshopify.com returned code 406

Your Link is bringing me to the section “Request a fulfillment” but I think I need to use the “Create a fulfillment” call right ?

So I used the API Call /admin/api/2023-04/fulfillment_orders/fulfillments.json"?
With the following information:

var options = {
“method” : “POST”,
“headers” : headers,

“fulfillment”:{
“line_items_by_fulfillment_order”:[
{
“fulfillment_order_id”: fulfillmentData.id,
“fulfillment_order_line_items”: fulfillmentData.line_items,
}]

}}

and the fulfillmentData is the object which was returned by the first api request ("

/admin/api/2023-04/orders/5691682128139/fulfillment_orders.json

")

What are the required parameters for the Call ? Error 406 means that the required parameters are not matching with the requested ones right ? But in the documentation for creating a fulfillment they just mentioned api_version and line_items_by_fulfillment_order are required parameters and in line_items_by_fulfillment_order just fulfillment_order_id: (integer) is required and if fulfillment_order_line_items: (array) is not provided it will fulfill all products (what is what we want anyway) so it should be fine to just provide the fulfillment_order_id right ? Why are the parameters not matching then ?

Hope you can help me again. Thanks a lot for the time you invest to answer my questions.

Here is the solution for my problem:
function createFulfillment(fulfillmentData){
readProperties();

var options = {
“method” : “POST”,
“contentType”:“application/json”,
“headers” : {
‘X-Shopify-Access-Token’: token
},
‘payload’ : JSON.stringify({
“fulfillment”:{
“line_items_by_fulfillment_order”:[
{
“fulfillment_order_id”: fulfillmentData.id
,
“fulfillment_order_line_items”: fulfillmentData.line_items,
}
]
}
})
};
console.log(fulfillmentData)
var response = UrlFetchApp.fetch(store + “/admin/api/2023-04/fulfillments.json”, options);
var jsResp = JSON.parse(response.getContentText());
Logger.log(jsResp);
}

Maybe it helps someone else

1 Like