{"tutorial":{"algolia":{"publishPending":false,"recordCount":4,"updatedAt":"2026-01-24T16:52:19.012Z"},"_id":"64dac0a1e94e5d00359ad9d5","title":"Fetch eligible shipment services","description":"Learn how to fetch shipment services.\n\nFind the full code here:\n[Python](https://github.com/amzn/selling-partner-api-samples/blob/main/use-cases/merchant-fulfillment/code/python/src/eligible_shipment_handler.py)\n[Java](https://github.com/amzn/selling-partner-api-samples/blob/main/use-cases/merchant-fulfillment/code/java/src/main/java/lambda/EligibleShipmentHandler.java)\n\n${MCF}$","emoji":"✅","backgroundColor":"#232f3e","referenceDisplay":[],"published":true,"hero":false,"slug":"fetch-eligible-shipment-services","response":"","previousSlug":"","slugUpdatedAt":"2023-08-14T21:00:44.117Z","revision":10,"steps":[{"title":"Initiate the GetEligibleShipmentServicesRequest","body":"To prepare the request to get eligible shipment services, enter details, such as the Amazon order ID, item list, ship-from address, package dimensions, weight, and default shipping service options that were previously set.","lineNumbers":["1-9","34-62"]},{"title":"Initiate the Merchant Fulfillment API client","body":"To access various operations within the Merchant Fulfillment API, initialize the Merchant Fulfillment API client and provide the region and refresh token as parameters.","lineNumbers":["13","17"]},{"title":"Execute the request","body":"To fetch the list of available shipment services, call the `getEligibleShipmentServices` method and pass in the request details initialized in Step 1.","lineNumbers":["14","23"]},{"title":"Process response details","body":"Parse and extract the list of shipping services and store them in a dedicated class. Finally, return the fetched results.","lineNumbers":["16-17","24-26"]}],"snippet":{"endpoint":{"method":"","slug":"","title":""},"codeOptions":[{"code":"// Get eligible shipment services for the order\nGetEligibleShipmentServicesRequest request = new GetEligibleShipmentServicesRequest()\n  .shipmentRequestDetails(new ShipmentRequestDetails()\n    .amazonOrderId(input.getOrderId())\n    .itemList(getItemList(mfnOrder.getOrderItems()))\n    .shipFromAddress(mfnOrder.getShipFromAddress())\n    .packageDimensions(mfnOrder.getPackageDimensions())\n    .weight(mfnOrder.getWeight())\n    .shippingServiceOptions(getDefaultShippingServiceOptions()));\n\nlogger.log(\"API Request: \" + new Gson().toJson(request));\n\nMerchantFulfillmentApi mfnApi = getMFNApi(input.getRegionCode(), input.getRefreshToken());\nGetEligibleShipmentServicesResponse response = mfnApi.getEligibleShipmentServices(request);\n\nmfnOrder.setShippingServiceList(response.getPayload().getShippingServiceList());\nreturn mfnOrder;","highlightedSyntax":"java","language":"java","name":"Java"},{"code":"def lambda_handler(event, context):\n    # Extract refresh token from event\n    refresh_token = event[constants.STATE_MACHINE_REFRESH_TOKEN_KEY_NAME]\n\n    # Extract region code from event\n    region_code = event[constants.STATE_MACHINE_REGION_CODE_KEY_NAME]\n\n    # Extract order ID from event\n    order_id = event[constants.STATE_MACHINE_ORDER_ID_KEY_NAME]\n\n    logger.info(f\"EligibleShipment Lambda input: {event}\")\n\n    try:\n        mfn_order = MfnOrder.from_dict(event[constants.MFN_ORDER_KEY_NAME])\n\n        # Create an instance of the ApiUtils class\n        api_utils = ApiUtils(refresh_token, region_code, constants.MFN_API_TYPE)\n\n        # Get eligible shipment services for the order\n        eligible_shipment_request = get_eligible_shipment_request_body(mfn_order=mfn_order, order_id=order_id)\n        logger.info(f\"Merchant Fulfillment API - GetEligibleShipmentServices request: {eligible_shipment_request}\")\n\n        eligible_shipment_response = api_utils.call_mfn_api('get_eligible_shipment_services', body=eligible_shipment_request)\n        mfn_order.shippingServiceList = [item for item in eligible_shipment_response.payload.shipping_service_list]\n\n        return mfn_order.to_json()\n\n    except json.JSONDecodeError as e:\n        raise Exception(\"Message body could not be mapped to MfnOrder\") from e\n\n    except Exception as e:\n        raise Exception(\"Calling Merchant Fulfillment API failed\") from e\n\n\ndef get_eligible_shipment_request_body(mfn_order, order_id):\n    order_item_list = json.loads(mfn_utils.transform_keys_to_uppercase_first_letter(json.dumps([item.to_json() for item in mfn_order.orderItems])))\n    ship_from_address = json.loads(mfn_utils.snake_to_pascal_case(json.dumps(mfn_order.shipFromAddress.to_dict())))\n    package_dimensions = json.loads(mfn_utils.snake_to_pascal_case(json.dumps(mfn_order.packageDimensions.to_dict())))\n    weight = json.loads(mfn_utils.snake_to_pascal_case(json.dumps(mfn_order.weight.to_dict())))\n\n    shipping_service_options = json.loads(mfn_utils.snake_to_pascal_case(json.dumps(get_default_shipping_service_options().to_dict())))\n\n    request = {\n        \"ShipmentRequestDetails\": {\n            \"AmazonOrderId\": order_id,\n            \"ItemList\": order_item_list,\n            \"ShipFromAddress\": ship_from_address,\n            \"PackageDimensions\": package_dimensions,\n            \"Weight\": weight,\n            \"ShippingServiceOptions\": shipping_service_options\n        }\n    }\n\n    return json.dumps(request)\n\n\ndef get_default_shipping_service_options():\n    return ShippingServiceOptions(delivery_experience=mfn_utils.DELIVERY_EXPERIENCE_TYPE,\n                                  carrier_will_pick_up=mfn_utils.CARRIER_WILL_PICK_UP,\n                                  label_format=mfn_utils.LABEL_FORMAT,\n                                  carrier_will_pick_up_option=None,\n                                  declared_value=None)","highlightedSyntax":"python","language":"python","name":"Python"}]},"project":"615737a581405300f2649fbd","version":"64da85507e9e01003fb6c349","createdAt":"2023-08-15T00:02:41.637Z","updatedAt":"2026-01-24T16:52:19.012Z","order":2,"__v":1,"stepCount":4,"id":"64dac0a1e94e5d00359ad9d5"}}