|
1
|
{"slug":"/references/fulfillment/provider","title":"How to Create a Fulfillment Module Provider","frontmatter":{"slug":"/references/fulfillment/provider","tags":["fulfillment","server","how to"],"sidebar_label":"Create Fulfillment Provider","keywords":["fulfillment","shipping","provider","integration"]},"toc":[{"title":"Understanding Fulfillment Module Provider Implementation","id":"understanding-fulfillment-module-provider-implementation","level":2},{"title":"1. Create Module Provider Directory","id":"1-create-module-provider-directory","level":2},{"title":"2. Create the Fulfillment Module Provider Service","id":"2-create-the-fulfillment-module-provider-service","level":2},{"title":"constructor","id":"constructor","level":3},{"title":"Example","id":"example","level":4},{"title":"identifier","id":"identifier","level":3},{"title":"Example","id":"example-1","level":4},{"title":"calculatePrice","id":"calculateprice","level":3},{"title":"When is this method invoked?","id":"when-is-this-method-invoked","level":3},{"title":"Example","id":"example-2","level":4},{"title":"Parameters","id":"calculateprice-parameters","level":4},{"title":"Returns","id":"calculateprice-returns","level":4},{"title":"canCalculate","id":"cancalculate","level":3},{"title":"Example","id":"example-3","level":4},{"title":"Parameters","id":"cancalculate-parameters","level":4},{"title":"Returns","id":"cancalculate-returns","level":4},{"title":"cancelFulfillment","id":"cancelfulfillment","level":3},{"title":"Example","id":"example-4","level":4},{"title":"Parameters","id":"cancelfulfillment-parameters","level":4},{"title":"Returns","id":"cancelfulfillment-returns","level":4},{"title":"createFulfillment","id":"createfulfillment","level":3},{"title":"Example","id":"example-5","level":4},{"title":"Parameters","id":"createfulfillment-parameters","level":4},{"title":"Returns","id":"createfulfillment-returns","level":4},{"title":"createReturnFulfillment","id":"createreturnfulfillment","level":3},{"title":"Example","id":"example-6","level":4},{"title":"Parameters","id":"createreturnfulfillment-parameters","level":4},{"title":"Returns","id":"createreturnfulfillment-returns","level":4},{"title":"getFulfillmentDocuments","id":"getfulfillmentdocuments","level":3},{"title":"Example","id":"example-7","level":4},{"title":"Parameters","id":"getfulfillmentdocuments-parameters","level":4},{"title":"Returns","id":"getfulfillmentdocuments-returns","level":4},{"title":"getFulfillmentOptions","id":"getfulfillmentoptions","level":3},{"title":"Example","id":"example-8","level":4},{"title":"Returns","id":"getfulfillmentoptions-returns","level":4},{"title":"getReturnDocuments","id":"getreturndocuments","level":3},{"title":"Example","id":"example-9","level":4},{"title":"Parameters","id":"getreturndocuments-parameters","level":4},{"title":"Returns","id":"getreturndocuments-returns","level":4},{"title":"getShipmentDocuments","id":"getshipmentdocuments","level":3},{"title":"Example","id":"example-10","level":4},{"title":"Parameters","id":"getshipmentdocuments-parameters","level":4},{"title":"Returns","id":"getshipmentdocuments-returns","level":4},{"title":"retrieveDocuments","id":"retrievedocuments","level":3},{"title":"Example","id":"example-11","level":4},{"title":"Parameters","id":"retrievedocuments-parameters","level":4},{"title":"Returns","id":"retrievedocuments-returns","level":4},{"title":"validateFulfillmentData","id":"validatefulfillmentdata","level":3},{"title":"Example","id":"example-12","level":4},{"title":"Parameters","id":"validatefulfillmentdata-parameters","level":4},{"title":"Returns","id":"validatefulfillmentdata-returns","level":4},{"title":"validateOption","id":"validateoption","level":3},{"title":"Example","id":"example-13","level":4},{"title":"Parameters","id":"validateoption-parameters","level":4},{"title":"Returns","id":"validateoption-returns","level":4},{"title":"3. Create Module Provider Definition File","id":"3-create-module-provider-definition-file","level":2},{"title":"4. Use Module Provider","id":"4-use-module-provider","level":2},{"title":"5. Test it Out","id":"5-test-it-out","level":2}],"blocks":[{"kind":"markdown","html":"In this document, you’ll learn how to create a Fulfillment Module Provider and the methods you must implement in its main service."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Understanding Fulfillment Module Provider Implementation","id":"understanding-fulfillment-module-provider-implementation"},{"kind":"markdown","html":"The Fulfillment Module Provider handles processing fulfillments and shipments with a third-party provider. However, it's not responsible for managing fulfillment concepts within Medusa, such as creating a fulfillment or its shipments. The Fulfillment Module uses your Fulfillment Module Provider within core operations.\n\nFor example, when the merchant creates a fulfillment for an order, the Fulfillment Module uses your Fulfillment Module Provider to create the fulfillment in the third-party system, then creates the fulfillment in Medusa. So, you only have to implement the third-party fulfillment processing logic in your Fulfillment Module Provider.\n\n\n---"},{"kind":"heading","level":2,"text":"1. Create Module Provider Directory","id":"1-create-module-provider-directory"},{"kind":"markdown","html":"Start by creating a new directory for your module provider.\n\nIf you're creating the module provider in a Medusa application, create it under the `src/modules` directory. For example, `src/modules/my-fulfillment`.\n\nIf you're creating the module provider in a plugin, create it under the `src/providers` directory. For example, `src/providers/my-fulfillment`."},{"kind":"note","variant":"note","html":"The rest of this guide always uses the `src/modules/my-fulfillment` directory as an example."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"2. Create the Fulfillment Module Provider Service","id":"2-create-the-fulfillment-module-provider-service"},{"kind":"markdown","html":"Create the file `src/modules/my-fulfillment/service.ts` that holds the module provider's main service. It must extend the `AbstractFulfillmentProviderService` class imported from `@medusajs/framework/utils`:\n\n```ts title=\"src/modules/my-fulfillment/service.ts\"\nimport { AbstractFulfillmentProviderService } from \"@medusajs/framework/utils\"\n\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // TODO implement methods\n}\n\nexport default MyFulfillmentProviderService\n```"},{"kind":"heading","level":3,"text":"constructor","id":"constructor"},{"kind":"markdown","html":"The constructor allows you to access resources from the [module's container](https://docs.medusajs.com/learn/fundamentals/modules/container)\nusing the first parameter, and the module's options using the second parameter."},{"kind":"note","variant":"note","html":"A module's options are passed when you register it in the Medusa application."},{"kind":"markdown","html":"If you're creating a client or establishing a connection with a third-party service, do it in the constructor."},{"kind":"heading","level":4,"text":"Example","id":"example"},{"kind":"markdown","html":"```ts title=\"src/modules/my-fulfillment/service.ts\"\nimport { AbstractFulfillmentProviderService } from \"@medusajs/framework/utils\"\nimport { Logger } from \"@medusajs/framework/types\"\n\ntype InjectedDependencies = {\n logger: Logger\n}\n\ntype Options = {\n apiKey: string\n}\n\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n// other properties...\n protected logger_: Logger\n protected options_: Options\n // assuming you're initializing a client\n protected client\n\n constructor(\n { logger }: InjectedDependencies,\n options: Options\n ) {\n super()\n\n this.logger_ = logger\n this.options_ = options\n\n // TODO initialize your client\n }\n}\n\nexport default MyFulfillmentProviderService\n```"},{"kind":"heading","level":3,"text":"identifier","id":"identifier"},{"kind":"markdown","html":"Each fulfillment provider has a unique identifier defined in its class. The provider's ID\nwill be stored as `fp_{identifier}_{id}`, where `{id}` is the provider's `id`\nproperty in the `medusa-config.ts`."},{"kind":"heading","level":4,"text":"Example","id":"example-1"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n static identifier = \"my-fulfillment\"\n\n // ...\n}\n```"},{"kind":"heading","level":3,"text":"calculatePrice","id":"calculateprice"},{"kind":"markdown","html":"This method calculates the price of a shipping method whose shipping option has the\n`price_type` set to `calculated`.\n\nIn this method, you can send a request to your third-party provider to retrieve the prices. The first\nparameters holds the `data` property of the shipping method's shipping option, which has fulfillment\nobject data returned by [getFulfillmentOptions](/references/fulfillment/provider#getfulfillmentoptions).\n\nThe second parameter holds the `data` property of the shipping method, which has data returned by [validateFulfillmentData](/references/fulfillment/provider#validatefulfillmentdata).\nIt can also hold custom data passed from the frontend during checkout.\n\nSo, using both of these data, assuming you're storing in them data related to the third-party service,\nyou can retrieve the calculated price of the shipping method."},{"kind":"heading","level":3,"text":"When is this method invoked?","id":"when-is-this-method-invoked"},{"kind":"markdown","html":"This method is invoked in Medusa whenever the calculated shipping option's price needs to be resolved\nagainst a cart:\n\n- When retrieving a cart's shipping options with their prices, such as when the customer views their shipping options during checkout.\n- When the shipping method is added to the cart.\n- Every time the cart is refreshed. A cart is refreshed after most cart changes, such as adding a line item or updating the cart's details, so this method may be invoked frequently for a cart that has a calculated shipping method.\n\nThe price isn't re-calculated when the cart is completed and converted to an order; the order uses the last price that was stored on the shipping method.\n\nIf this method throws an error, or returns a result without a `calculated_amount`, the shipping\noption's price can't be resolved. This blocks the operation that triggered the calculation,\nsuch as adding the shipping method to the cart or the cart refresh triggered by other cart\nchanges, which can prevent the customer from completing checkout. So, ensure to handle errors\ngracefully, such as by falling back to a default price, if you don't want a failure in the\nthird-party service to block checkout."},{"kind":"heading","level":4,"text":"Example","id":"example-2"},{"kind":"markdown","html":"```ts\nimport { CalculateShippingOptionPriceDTO } from \"@medusajs/framework/types\"\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async calculatePrice(\n optionData: CalculateShippingOptionPriceDTO[\"optionData\"],\n data: CalculateShippingOptionPriceDTO[\"data\"],\n context: CalculateShippingOptionPriceDTO[\"context\"]\n ): Promise<CalculatedShippingOptionPrice> {\n // assuming the client can calculate the price using\n // the third-party service\n const price = await this.client.calculate(data, context)\n return {\n calculated_amount: price,\n // Update this boolean value based on your logic\n is_calculated_price_tax_inclusive: true,\n }\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"calculateprice-parameters"},{"kind":"typeList","sectionTitle":"calculatePrice","types":[{"name":"optionData","type":"`Record<string, unknown>`","description":"The `data` property of a shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"data","type":"`Record<string, unknown>`","description":"The shipping method's `data` property with custom data passed from the frontend.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[CartPropsForFulfillment](/references/fulfillment/types/fulfillment.CartPropsForFulfillment) & { [k: string]: unknown; from\\_location?: StockLocationDTO \\| undefined; } & CalculatedRMAShippingContext","description":"The context details, such as the cart details. If the workflow that triggered\nthe calculation consumed the `setCalculatedShippingPricingContext` hook, its returned properties\nare merged into this object as well, with the framework-provided properties always taking\nprecedence if there's a naming conflict.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"[CartDTO](/references/fulfillment/interfaces/fulfillment.CartDTO)[`\"id\"`]","description":"The cart's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_address","type":"[CartDTO](/references/fulfillment/interfaces/fulfillment.CartDTO)[`\"shipping_address\"`]","description":"The cart's shipping address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"items","type":"[CartLineItemDTO](/references/fulfillment/interfaces/fulfillment.CartLineItemDTO) & `object`[]","description":"The cart's items","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"original_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The original total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"original_subtotal","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The original subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"original_tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The original tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"item_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The item total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"item_subtotal","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The item subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"item_tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The item tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"subtotal","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discount_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The discount total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discount_tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The discount tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_original_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw original total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_original_subtotal","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw original subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_original_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw original tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_item_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw item total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_item_subtotal","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw item subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_item_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw item tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_subtotal","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_discount_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw discount total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_discount_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw discount tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"id","type":"`string`","description":"The ID of the line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"title","type":"`string`","description":"The title of the line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"subtitle","type":"`string`","description":"The subtitle of the line item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"thumbnail","type":"`string`","description":"The line item's thumbnail.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"quantity","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The line item's quantity in the cart.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"product_id","type":"`string`","description":"The ID of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_title","type":"`string`","description":"The title of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_description","type":"`string`","description":"The description of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_subtitle","type":"`string`","description":"The subtitle of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_type","type":"`string`","description":"The type of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_type_id","type":"`string`","description":"The type of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_collection","type":"`string`","description":"The collection of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_handle","type":"`string`","description":"The handle of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_id","type":"`string`","description":"The associated variant's ID of the line item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_sku","type":"`string`","description":"The sku of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_barcode","type":"`string`","description":"The barcode of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_title","type":"`string`","description":"The title of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_option_values","type":"`Record<string, unknown>`","description":"The option values of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"requires_shipping","type":"`boolean`","description":"Whether the line item requires shipping.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_discountable","type":"`boolean`","description":"Whether the line item is discountable.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_giftcard","type":"`boolean`","description":"Whether the line item is a gift card.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_tax_inclusive","type":"`boolean`","description":"Whether the line item price is tax inclusive.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_custom_price","type":"`boolean`","description":"Whether the line item price is a custom price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"compare_at_unit_price","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The calculated price of the line item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"unit_price","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The unit price of the item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tax_lines","type":"[LineItemTaxLineDTO](/references/fulfillment/interfaces/fulfillment.LineItemTaxLineDTO)[]","description":"The associated tax lines.","optional":true,"defaultValue":"","expandable":true,"children":[]},{"name":"adjustments","type":"[LineItemAdjustmentDTO](/references/fulfillment/interfaces/fulfillment.LineItemAdjustmentDTO)[]","description":"The associated adjustments.","optional":true,"defaultValue":"","expandable":true,"children":[]},{"name":"cart","type":"[CartDTO](/references/fulfillment/interfaces/fulfillment.CartDTO)","description":"The associated cart.","optional":false,"defaultValue":"","expandable":true,"children":[]},{"name":"cart_id","type":"`string`","description":"The ID of the associated cart.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`Date`","description":"When the line item was created.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`Date`","description":"When the line item was updated.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`Date`","description":"When the line item was deleted.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant","type":"`object`","description":"The item's variant.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"product","type":"`object`","description":"The item's product.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"calculateprice-returns"},{"kind":"typeList","sectionTitle":"calculatePrice","types":[{"name":"Promise","type":"Promise<[CalculatedShippingOptionPrice](/references/types/types/types.CalculatedShippingOptionPrice)>","optional":false,"defaultValue":"","description":"The calculated price's details.","expandable":false,"children":[{"name":"calculated_amount","type":"`number`","description":"The calculated price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_calculated_price_tax_inclusive","type":"`boolean`","description":"Whether the calculated price includes taxes. If enabled, Medusa will\ninfer the taxes from the calculated price. If false, Medusa will\nadd taxes to the calculated price.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"canCalculate","id":"cancalculate"},{"kind":"markdown","html":"This method validates whether a shippin option's price can be calculated during checkout. It's executed when the admin user creates a shipping\noption of type `calculated`. If this method returns `false`, an error is thrown as the shipping option's price can't be calculated.\n\nYou can perform the checking using the third-party provider if applicable. The `data` parameter will hold the shipping option's `data` property, which\nincludes the data of a fulfillment option returned by [getFulfillmentOptions](/references/fulfillment/provider#getfulfillmentoptions)."},{"kind":"heading","level":4,"text":"Example","id":"example-3"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async canCalculate(data: CreateShippingOptionDTO): Promise<boolean> {\n // assuming you have a client\n return await this.client.hasRates(data.id)\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"cancalculate-parameters"},{"kind":"typeList","sectionTitle":"canCalculate","types":[{"name":"data","type":"[CreateShippingOptionDTO](/references/fulfillment/interfaces/fulfillment.CreateShippingOptionDTO)","description":"The `data` property of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"name","type":"`string`","description":"The name of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"price_type","type":"[ShippingOptionPriceType](/references/fulfillment/types/fulfillment.ShippingOptionPriceType)","description":"The type of the shipping option's price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"service_zone_id","type":"`string`","description":"The associated service zone's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_profile_id","type":"`string`","description":"The associated shipping profile's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"provider_id","type":"`string`","description":"The associated provider's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"type","type":"`string` \\| [CreateShippingOptionTypeDTO](/references/fulfillment/interfaces/fulfillment.CreateShippingOptionTypeDTO)","description":"The shipping option type associated with the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"label","type":"`string`","description":"The label of the shipping option type.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"description","type":"`string`","description":"The description of the shipping option type.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"code","type":"`string`","description":"The code of the shipping option type.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"data","type":"`null` \\| `Record<string, unknown>`","description":"The data necessary for the associated fulfillment provider to process the shipping option\nand its associated fulfillments.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"rules","type":"Omit<[CreateShippingOptionRuleDTO](/references/fulfillment/interfaces/fulfillment.CreateShippingOptionRuleDTO), \"shipping_option_id\">[]","description":"The shipping option rules associated with the shipping option.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"attribute","type":"`string`","description":"The attribute of the shipping option rule.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"operator","type":"[RuleOperatorType](/references/fulfillment/types/fulfillment.RuleOperatorType)","description":"The operator of the shipping option rule.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"value","type":"`string` \\| `string`[]","description":"The value(s) of the shipping option rule.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"cancalculate-returns"},{"kind":"typeList","sectionTitle":"canCalculate","types":[{"name":"Promise","type":"Promise<boolean>","optional":false,"defaultValue":"","description":"Whether the price can be calculated for the shipping option.","expandable":false,"children":[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"cancelFulfillment","id":"cancelfulfillment"},{"kind":"markdown","html":"This method is used when a fulfillment is canceled. Use it to perform operations\nwith the third-party fulfillment service."},{"kind":"heading","level":4,"text":"Example","id":"example-4"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async cancelFulfillment(data: Record<string, unknown>): Promise<any> {\n // assuming the client cancels a fulfillment\n // in the third-party service\n const { external_id } = data as {\n external_id: string\n }\n await this.client.cancel(external_id)\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"cancelfulfillment-parameters"},{"kind":"typeList","sectionTitle":"cancelFulfillment","types":[{"name":"data","type":"`Record<string, unknown>`","description":"The fulfillment's `data` property.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"cancelfulfillment-returns"},{"kind":"typeList","sectionTitle":"cancelFulfillment","types":[{"name":"Promise","type":"Promise<any>","optional":false,"defaultValue":"","description":"This method is used when a fulfillment is canceled. Use it to perform operations\nwith the third-party fulfillment service.","expandable":false,"children":[{"name":"any","type":"`any`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"createFulfillment","id":"createfulfillment"},{"kind":"markdown","html":"This method is used when a fulfillment is created. If the method returns in the object a\n`data` property, it's stored in the fulfillment's `data` property.\n\nThe `data` property is useful when handling the fulfillment later,\nas you can access information useful for your integration, such as the ID in the\nthird-party provider.\n\nYou can also use this method to perform an action with the third-party fulfillment service\nsince a fulfillment is created, such as purchase a label."},{"kind":"heading","level":4,"text":"Example","id":"example-5"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async createFulfillment(\n data: Record<string, unknown>,\n items: Partial<Omit<FulfillmentItemDTO, \"fulfillment\">>[],\n order: Partial<FulfillmentOrderDTO> | undefined,\n fulfillment: Partial<Omit<FulfillmentDTO, \"provider_id\" | \"data\" | \"items\">>,\n additionalData?: Record<string, unknown>\n ): Promise<CreateFulfillmentResult> {\n // assuming the client creates a fulfillment\n // in the third-party service\n const externalData = await this.client.create(\n fulfillment,\n items\n )\n\n return {\n data: {\n ...(fulfillment.data as object || {}),\n ...externalData\n }\n }\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"createfulfillment-parameters"},{"kind":"typeList","sectionTitle":"createFulfillment","types":[{"name":"data","type":"`Record<string, unknown>`","description":"The `data` property of the shipping method this fulfillment is created for.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"items","type":"Partial<Omit<[FulfillmentItemDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentItemDTO), \"fulfillment\">>[]","description":"The items in the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"order","type":"`undefined` \\| Partial<FulfillmentOrderDTO>","description":"The order this fulfillment is created for.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillment","type":"Partial<Omit<[FulfillmentDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentDTO), \"data\" \\| \"items\" \\| \"provider_id\">>","description":"The fulfillment's details.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"location_id","type":"`string`","description":"The associated location's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"packed_at","type":"`null` \\| `Date`","description":"The date the fulfillment was packed.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipped_at","type":"`null` \\| `Date`","description":"The date the fulfillment was shipped.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"delivered_at","type":"`null` \\| `Date`","description":"The date the fulfillment was delivered.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"canceled_at","type":"`null` \\| `Date`","description":"The date the fulfillment was canceled.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"marked_shipped_by","type":"`null` \\| `string`","description":"The id of the user that marked fulfillment as shipped","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"created_by","type":"`null` \\| `string`","description":"The id of the user that created the fulfillment","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_option_id","type":"`null` \\| `string`","description":"The associated shipping option's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_option","type":"`null` \\| [ShippingOptionDTO](/references/fulfillment/interfaces/fulfillment.ShippingOptionDTO)","description":"The associated shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"name","type":"`string`","description":"The name of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"price_type","type":"[ShippingOptionPriceType](/references/fulfillment/types/fulfillment.ShippingOptionPriceType)","description":"The type of the shipping option's price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"service_zone_id","type":"`string`","description":"The associated service zone's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_profile_id","type":"`string`","description":"The associated shipping profile's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"provider_id","type":"`string`","description":"The associated fulfillment provider's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_option_type_id","type":"`null` \\| `string`","description":"The associated shipping option type's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"data","type":"`null` \\| `Record<string, unknown>`","description":"The data necessary for the associated fulfillment provider to process the shipping option\nand, later, its associated fulfillments.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"service_zone","type":"[ServiceZoneDTO](/references/fulfillment/interfaces/fulfillment.ServiceZoneDTO)","description":"The associated service zone.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_profile","type":"[ShippingProfileDTO](/references/fulfillment/interfaces/fulfillment.ShippingProfileDTO)","description":"The associated shipping profile.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillment_provider","type":"[FulfillmentProviderDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentProviderDTO)","description":"The associated fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"type","type":"[ShippingOptionTypeDTO](/references/fulfillment/interfaces/fulfillment.ShippingOptionTypeDTO)","description":"The associated shipping option type.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"rules","type":"[ShippingOptionRuleDTO](/references/fulfillment/interfaces/fulfillment.ShippingOptionRuleDTO)[]","description":"The rules associated with the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillments","type":"[FulfillmentDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentDTO)[]","description":"The fulfillments associated with the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`Date`","description":"The creation date of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`Date`","description":"The update date of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`null` \\| `Date`","description":"The deletion date of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"requires_shipping","type":"`boolean`","description":"Flag to indidcate whether shipping is required","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"provider","type":"[FulfillmentProviderDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentProviderDTO)","description":"The associated fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"name","type":"`string`","description":"The name of the fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_options","type":"[ShippingOptionDTO](/references/fulfillment/interfaces/fulfillment.ShippingOptionDTO)[]","description":"The shipping options associated with the fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`Date`","description":"The creation date of the fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`Date`","description":"The update date of the fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`null` \\| `Date`","description":"The deletion date of the fulfillment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"delivery_address","type":"[FulfillmentAddressDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentAddressDTO)","description":"The associated fulfillment address used for delivery.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillment_id","type":"`null` \\| `string`","description":"The associated fulfillment's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"company","type":"`null` \\| `string`","description":"The company of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"first_name","type":"`null` \\| `string`","description":"The first name of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"last_name","type":"`null` \\| `string`","description":"The last name of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"address_1","type":"`null` \\| `string`","description":"The first line of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"address_2","type":"`null` \\| `string`","description":"The second line of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"city","type":"`null` \\| `string`","description":"The city of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"country_code","type":"`null` \\| `string`","description":"The ISO 2 character country code of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"province","type":"`null` \\| `string`","description":"The lower-case [ISO 3166-2](https://en.wikipedia.org/wiki/ISO\\_3166-2) province of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"postal_code","type":"`null` \\| `string`","description":"The postal code of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"phone","type":"`null` \\| `string`","description":"The phone of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`Date`","description":"The creation date of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`Date`","description":"The update date of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`null` \\| `Date`","description":"The deletion date of the address.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"labels","type":"[FulfillmentLabelDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentLabelDTO)[]","description":"The labels of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tracking_number","type":"`string`","description":"The tracking number of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tracking_url","type":"`string`","description":"The tracking URL of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"label_url","type":"`string`","description":"The label's URL.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillment_id","type":"`string`","description":"The associated fulfillment's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillment","type":"[FulfillmentDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentDTO)","description":"The associated fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`Date`","description":"The creation date of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`Date`","description":"The update date of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`null` \\| `Date`","description":"The deletion date of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"created_at","type":"`Date`","description":"The creation date of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`Date`","description":"The update date of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`null` \\| `Date`","description":"The deletion date of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"additionalData","type":"`Record<string, unknown>`","description":"Custom key-value pairs forwarded from the workflow that\ncreated the fulfillment (for example, the `additional_data` of the Create Order\nFulfillment API route). It isn't persisted on the fulfillment.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"createfulfillment-returns"},{"kind":"typeList","sectionTitle":"createFulfillment","types":[{"name":"Promise","type":"Promise<[CreateFulfillmentResult](/references/types/types/types.CreateFulfillmentResult)>","optional":false,"defaultValue":"","description":"An object whose `data` property is stored in the fulfillment's `data` property.","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Additional fulfillment data from provider","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"labels","type":"`object`[]","description":"","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"tracking_number","type":"`string`","description":"The tracking number of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tracking_url","type":"`string`","description":"The tracking URL of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"label_url","type":"`string`","description":"The label's URL.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":3,"text":"createReturnFulfillment","id":"createreturnfulfillment"},{"kind":"markdown","html":"This method is used when a fulfillment is created for a return. If the method returns in the object a\n`data` property, it's stored in the fulfillment's `data` property.\n\nThe `data` property is useful when handling the fulfillment later,\nas you can access information useful for your integration. For example, you\ncan store an ID for the fulfillment in the third-party service.\n\nUse this method to perform actions necessary in the third-party fulfillment service, such as\npurchasing a label for the return fulfillment."},{"kind":"heading","level":4,"text":"Example","id":"example-6"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async createReturnFulfillment(fulfillment: Record<string, unknown>): Promise<CreateFulfillmentResult> {\n // assuming the client creates a fulfillment for a return\n // in the third-party service\n const externalData = await this.client.createReturn(\n fulfillment\n )\n\n return {\n data: {\n ...(fulfillment.data as object || {}),\n ...externalData\n }\n }\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"createreturnfulfillment-parameters"},{"kind":"typeList","sectionTitle":"createReturnFulfillment","types":[{"name":"fulfillment","type":"`Record<string, unknown>`","description":"The fulfillment's details.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"createreturnfulfillment-returns"},{"kind":"typeList","sectionTitle":"createReturnFulfillment","types":[{"name":"Promise","type":"Promise<[CreateFulfillmentResult](/references/types/types/types.CreateFulfillmentResult)>","optional":false,"defaultValue":"","description":"An object containing `data` which is stored in the fulfillment's `data` property and `labels` array which is used to create FulfillmentLabels.","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Additional fulfillment data from provider","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"labels","type":"`object`[]","description":"","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"tracking_number","type":"`string`","description":"The tracking number of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tracking_url","type":"`string`","description":"The tracking URL of the fulfillment label.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"label_url","type":"`string`","description":"The label's URL.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":3,"text":"getFulfillmentDocuments","id":"getfulfillmentdocuments"},{"kind":"markdown","html":"This method retrieves the documents of a fulfillment."},{"kind":"heading","level":4,"text":"Example","id":"example-7"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async getFulfillmentDocuments(data: any): Promise<never[]> {\n // assuming the client retrieves documents\n // from a third-party service\n return await this.client.documents(data)\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"getfulfillmentdocuments-parameters"},{"kind":"typeList","sectionTitle":"getFulfillmentDocuments","types":[{"name":"data","type":"`Record<string, unknown>`","description":"The `data` property of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"getfulfillmentdocuments-returns"},{"kind":"typeList","sectionTitle":"getFulfillmentDocuments","types":[{"name":"Promise","type":"Promise<never[]>","optional":false,"defaultValue":"","description":"The fulfillment's documents.","expandable":false,"children":[{"name":"never[]","type":"`never`[]","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"getFulfillmentOptions","id":"getfulfillmentoptions"},{"kind":"markdown","html":"This method retrieves a list of fulfillment options that this provider supports. Admin users will then choose from these options when\nthey're creating a shipping option. The chosen fulfillment option's object is then stored within the created shipping option's `data` property.\nThe `data` property is useful to store data relevant for the third-party provider to later process the fulfillment.\n\nThis method is useful if your third-party provider allows you to retrieve support options, carriers, or services from an API. You can then\nretrieve those and return then in the method, allowing the admin user to choose from the services provided by the third-party provider."},{"kind":"heading","level":4,"text":"Example","id":"example-8"},{"kind":"markdown","html":"```ts\n// other imports...\nimport { FulfillmentOption } from \"@medusajs/framework/types\"\n\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async getFulfillmentOptions(): Promise<FulfillmentOption[]> {\n // assuming you have a client\n const services = await this.client.getServices()\n\n return services.map((service) => ({\n id: service.service_id,\n name: service.name,\n service_code: service.code,\n // can add other relevant data for the provider to later process the shipping option.\n }))\n }\n}\n```"},{"kind":"heading","level":4,"text":"Returns","id":"getfulfillmentoptions-returns"},{"kind":"typeList","sectionTitle":"getFulfillmentOptions","types":[{"name":"Promise","type":"Promise<[FulfillmentOption](/references/types/types/types.FulfillmentOption)[]>","optional":false,"defaultValue":"","description":"The list of fulfillment options. Each object in the array should have an `id` property unique to an item, and a `name` property\nthat's used to display the option in the admin.","expandable":false,"children":[{"name":"FulfillmentOption[]","type":"[FulfillmentOption](/references/types/types/types.FulfillmentOption)[]","optional":false,"defaultValue":"","description":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The option's ID. This ID can be an ID in the third-party system relevant\nfor later processing of fulfillment.","optional":false,"defaultValue":"","example":"express","expandable":false,"children":[]},{"name":"is_return","type":"`boolean`","description":"Whether the option can be used to return items.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":3,"text":"getReturnDocuments","id":"getreturndocuments"},{"kind":"markdown","html":"This method retrieves documents for a return's fulfillment."},{"kind":"heading","level":4,"text":"Example","id":"example-9"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async getReturnDocuments(data: any): Promise<never[]> {\n // assuming the client retrieves documents\n // from a third-party service\n return await this.client.documents(data)\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"getreturndocuments-parameters"},{"kind":"typeList","sectionTitle":"getReturnDocuments","types":[{"name":"data","type":"`Record<string, unknown>`","description":"The `data` property of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"getreturndocuments-returns"},{"kind":"typeList","sectionTitle":"getReturnDocuments","types":[{"name":"Promise","type":"Promise<never[]>","optional":false,"defaultValue":"","description":"The fulfillment's documents.","expandable":false,"children":[{"name":"never[]","type":"`never`[]","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"getShipmentDocuments","id":"getshipmentdocuments"},{"kind":"markdown","html":"This method retrieves the documents for a shipment."},{"kind":"heading","level":4,"text":"Example","id":"example-10"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async getShipmentDocuments(data: any): Promise<never[]> {\n // assuming the client retrieves documents\n // from a third-party service\n return await this.client.documents(data)\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"getshipmentdocuments-parameters"},{"kind":"typeList","sectionTitle":"getShipmentDocuments","types":[{"name":"data","type":"`Record<string, unknown>`","description":"The `data` property of the shipmnet.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"getshipmentdocuments-returns"},{"kind":"typeList","sectionTitle":"getShipmentDocuments","types":[{"name":"Promise","type":"Promise<never[]>","optional":false,"defaultValue":"","description":"The shipment's documents.","expandable":false,"children":[{"name":"never[]","type":"`never`[]","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"retrieveDocuments","id":"retrievedocuments"},{"kind":"markdown","html":"This method retrieves the documents of a fulfillment of a certain type."},{"kind":"heading","level":4,"text":"Example","id":"example-11"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async retrieveDocuments(\n fulfillmentData: any,\n documentType: any\n ): Promise<void> {\n // assuming the client retrieves documents\n // from a third-party service\n return await this.client.documents(\n fulfillmentData,\n documentType\n )\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"retrievedocuments-parameters"},{"kind":"typeList","sectionTitle":"retrieveDocuments","types":[{"name":"fulfillmentData","type":"`Record<string, unknown>`","description":"The `data` property of the fulfillment.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"documentType","type":"`string`","description":"The document's type. For example, `invoice`.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"retrievedocuments-returns"},{"kind":"typeList","sectionTitle":"retrieveDocuments","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"The fulfillment's documents.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"validateFulfillmentData","id":"validatefulfillmentdata"},{"kind":"markdown","html":"This method validates the `data` property of a shipping method and returns it. The returned data\nis stored in the shipping method's `data` property.\n\nYour fulfillment provider can use the `data` property to store additional information useful for\nhandling the fulfillment later. For example, you may store an ID from the third-party fulfillment\nsystem."},{"kind":"heading","level":4,"text":"Example","id":"example-12"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async validateFulfillmentData(\n optionData: any,\n data: any,\n context: any\n ): Promise<any> {\n // assuming your client retrieves an ID from the\n // third-party service\n const externalId = await this.client.getId()\n\n return {\n ...data,\n externalId\n }\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"validatefulfillmentdata-parameters"},{"kind":"typeList","sectionTitle":"validateFulfillmentData","types":[{"name":"optionData","type":"`Record<string, unknown>`","description":"The `data` property of the shipping option.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"data","type":"`Record<string, unknown>`","description":"The `data` property of the shipping method.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[ValidateFulfillmentDataContext](/references/types/types/types.ValidateFulfillmentDataContext)","description":"Context details, such as context of the cart or customer.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"[CartDTO](/references/fulfillment/interfaces/fulfillment.CartDTO)[`\"id\"`]","description":"The cart's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_address","type":"[CartDTO](/references/fulfillment/interfaces/fulfillment.CartDTO)[`\"shipping_address\"`]","description":"The cart's shipping address.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"items","type":"[CartLineItemDTO](/references/fulfillment/interfaces/fulfillment.CartLineItemDTO) & `object`[]","description":"The cart's items","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"original_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The original total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"original_subtotal","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The original subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"original_tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The original tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"item_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The item total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"item_subtotal","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The item subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"item_tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The item tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"subtotal","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discount_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The discount total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discount_tax_total","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The discount tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_original_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw original total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_original_subtotal","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw original subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_original_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw original tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_item_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw item total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_item_subtotal","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw item subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_item_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw item tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_subtotal","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw subtotal of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_discount_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw discount total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw_discount_tax_total","type":"[BigNumberRawValue](/references/fulfillment/types/fulfillment.BigNumberRawValue)","description":"The raw discount tax total of the cart line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"id","type":"`string`","description":"The ID of the line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"title","type":"`string`","description":"The title of the line item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"subtitle","type":"`string`","description":"The subtitle of the line item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"thumbnail","type":"`string`","description":"The line item's thumbnail.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"quantity","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The line item's quantity in the cart.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"product_id","type":"`string`","description":"The ID of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_title","type":"`string`","description":"The title of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_description","type":"`string`","description":"The description of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_subtitle","type":"`string`","description":"The subtitle of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_type","type":"`string`","description":"The type of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_type_id","type":"`string`","description":"The type of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_collection","type":"`string`","description":"The collection of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"product_handle","type":"`string`","description":"The handle of the associated product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_id","type":"`string`","description":"The associated variant's ID of the line item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_sku","type":"`string`","description":"The sku of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_barcode","type":"`string`","description":"The barcode of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_title","type":"`string`","description":"The title of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant_option_values","type":"`Record<string, unknown>`","description":"The option values of the associated variant.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"requires_shipping","type":"`boolean`","description":"Whether the line item requires shipping.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_discountable","type":"`boolean`","description":"Whether the line item is discountable.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_giftcard","type":"`boolean`","description":"Whether the line item is a gift card.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_tax_inclusive","type":"`boolean`","description":"Whether the line item price is tax inclusive.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_custom_price","type":"`boolean`","description":"Whether the line item price is a custom price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"compare_at_unit_price","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The calculated price of the line item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"unit_price","type":"[BigNumberValue](/references/fulfillment/types/fulfillment.BigNumberValue)","description":"The unit price of the item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tax_lines","type":"[LineItemTaxLineDTO](/references/fulfillment/interfaces/fulfillment.LineItemTaxLineDTO)[]","description":"The associated tax lines.","optional":true,"defaultValue":"","expandable":true,"children":[]},{"name":"adjustments","type":"[LineItemAdjustmentDTO](/references/fulfillment/interfaces/fulfillment.LineItemAdjustmentDTO)[]","description":"The associated adjustments.","optional":true,"defaultValue":"","expandable":true,"children":[]},{"name":"cart","type":"[CartDTO](/references/fulfillment/interfaces/fulfillment.CartDTO)","description":"The associated cart.","optional":false,"defaultValue":"","expandable":true,"children":[]},{"name":"cart_id","type":"`string`","description":"The ID of the associated cart.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`Date`","description":"When the line item was created.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`Date`","description":"When the line item was updated.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`Date`","description":"When the line item was deleted.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"variant","type":"`object`","description":"The item's variant.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"product","type":"`object`","description":"The item's product.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"from_location","type":"[StockLocationDTO](/references/types/StockLocationTypes/types/types.StockLocationTypes.StockLocationDTO)","description":"Details about the location that items are being shipped from.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the stock location.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"name","type":"`string`","description":"The name of the stock location.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`Record<string, unknown>` \\| `null`","description":"Holds custom data in key-value pairs.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"address_id","type":"`string`","description":"The associated address's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"address","type":"[StockLocationAddressDTO](/references/types/StockLocationTypes/types/types.StockLocationTypes.StockLocationAddressDTO)","description":"The address of the stock location.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillment_sets","type":"[FulfillmentSetDTO](/references/fulfillment/interfaces/fulfillment.FulfillmentSetDTO)[]","description":"Fulfillment sets for the location","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`string` \\| `Date`","description":"The creation date of the stock location.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`string` \\| `Date`","description":"The update date of the stock location.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`string` \\| `Date` \\| `null`","description":"The deletion date of the stock location.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"validatefulfillmentdata-returns"},{"kind":"typeList","sectionTitle":"validateFulfillmentData","types":[{"name":"Promise","type":"Promise<any>","optional":false,"defaultValue":"","description":"the data to store in the `data` property of the shipping method.","expandable":false,"children":[{"name":"any","type":"`any`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"validateOption","id":"validateoption"},{"kind":"markdown","html":"This method validates the `data` property of a shipping option when it's created.\n\nThe `data` property can hold useful information that's later added to the `data` attribute\nof shipping methods created from this option."},{"kind":"heading","level":4,"text":"Example","id":"example-13"},{"kind":"markdown","html":"```ts\nclass MyFulfillmentProviderService extends AbstractFulfillmentProviderService {\n // ...\n async validateOption(data: any): Promise<boolean> {\n return data.external_id !== undefined\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"validateoption-parameters"},{"kind":"typeList","sectionTitle":"validateOption","types":[{"name":"data","type":"`Record<string, unknown>`","description":"The data to validate.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Returns","id":"validateoption-returns"},{"kind":"typeList","sectionTitle":"validateOption","types":[{"name":"Promise","type":"Promise<boolean>","optional":false,"defaultValue":"","description":"Whether the data is valid.","expandable":false,"children":[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"3. Create Module Provider Definition File","id":"3-create-module-provider-definition-file"},{"kind":"markdown","html":"Create the file `src/modules/my-fulfillment/index.ts` with the following content:\n\n```ts title=\"src/modules/my-fulfillment/index.ts\"\nimport MyFulfillmentProviderService from \"./service\"\nimport { \n ModuleProvider, \n Modules\n} from \"@medusajs/framework/utils\"\n\nexport default ModuleProvider(Modules.FULFILLMENT, {\n services: [MyFulfillmentProviderService],\n})\n```\n\nThis exports the module provider's definition, indicating that the `MyFulfillmentProviderService` is the module provider's service."},{"kind":"note","variant":"note","title":"Tip","html":"A fulfillment module provider can have export multiple provider services, where each are registered as a separate fulfillment provider."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"4. Use Module Provider","id":"4-use-module-provider"},{"kind":"markdown","html":"To use your Fulfillment Module Provider, add it to the `providers` array of the Fulfillment Module in `medusa-config.ts`:\n\n```ts title=\"medusa-config.ts\"\nmodule.exports = defineConfig({\n // ...\n modules: [\n {\n resolve: \"@medusajs/medusa/fulfillment\",\n options: {\n providers: [\n // default provider\n {\n resolve: \"@medusajs/medusa/fulfillment-manual\",\n id: \"manual\",\n },\n {\n // if module provider is in a plugin, use `plugin-name/providers/my-fulfillment`\n resolve: \"./src/modules/my-fulfillment\",\n id: \"my-fulfillment\",\n options: {\n // provider options...\n },\n },\n ],\n },\n },\n ]\n})\n```\n\n\n---"},{"kind":"heading","level":2,"text":"5. Test it Out","id":"5-test-it-out"},{"kind":"markdown","html":"Before you use your Fulfillment Module Provider, in the Medusa Admin:\n\n1. Add the Fulfillment Module Provider to a location.\n2. Add in the location a delivery shipping option that uses the provider.\n\nThen, place an order, choosing the shipping option you created during checkout, and create a fulfillment in the Medusa Admin. The fulfillment is created using your provider."}]} |