createShipment()
This method creates a new shipment.
Syntax
module.exports = async function createShipment(transaction, shipment) { // Your code here}
import { Transaction, NewShipment, ShipmentConfirmation } from "@shipengine/connect";export default async function createShipment( transaction: Transaction, shipment: NewShipment): Promise<ShipmentConfirmation> { // Your code here}
Parameters
transaction
A transaction object containing information about the transaction and session state.
shipment
An object containing information about the new shipment to create.
Name | Type | Nullable? | Description |
---|---|---|---|
deliveryService | An object that identifies a delivery service that is offered by a carrier. | ||
shipFrom | The address from which the shipment is being shipped. | ||
shipTo | The address to which the shipment is being shipped. | ||
pickupLocation | ✔ | The location where the recipient can pick up the shipment. | |
returnTo | The address to which to send the shipment in the case of a return. This may be different from the | ||
shipDateTime | The date/time that the package is expected to ship. This is not guaranteed to be in the future. | ||
totalInsuredValue | object | The total insured value of all packages in the shipment. | |
totalInsuredValue.value | number | The value of the insured amount. | |
totalInsuredValue
.currency | string | The currency that the value represents. | |
isNonMachineable | boolean | Indicates whether the shipment cannot be processed automatically due to size, shape, weight, etc. and requires manual handling. This property is | |
returns | object | An object that indicates whether or not this shipment is a return shipment. | |
returns.isReturn | boolean | Indicates whether or not this shipment is a return shipment. | |
returns.rmaNumber | string | A return merchandise authorization (RMA) is an associated number assigned to process the return. This number is often printed on the label and used when the original shipper processes the inbound return. This string will not contain newline characters. | |
packages | The list of packages in this shipment. | ||
package | Returns the first package in the | ||
deliveryConfirmation | ✔ | The requested delivery confirmation for this shipment. | |
shippingOptions | ✔ | The shipping options that are required for this shipment. |
Return Value
shipmentConfirmation
An object that contains confirmation that a shipment has been created.
Name | Type | Required? | Description |
---|---|---|---|
label | object | ✔ | An object representing the shipping label details. |
label.name | string | The user-friendly name of the label. This string must not contain newline characters. | |
label.type | string | ✔ | The type of document. This value should always be |
label.size | string | ✔ | The size of the label. Valid values include the following:
|
label.format | string | ✔ | The file format of the label. Valid values include the following:
|
label.data | ✔ | The label data, in the specified file format. | |
label.referenceFields | string[] | ✔ | The actual reference fields on the label, which may not match the originally-specified reference fields due to the carrier's restrictions on the number of fields or the length of each field. Each string in this array must not contain newline characters. |
charges | ✔ | The breakdown of charges for this shipment. If the carrier does not provide a detailed breakdown, then just use a single charge of type "shipping". | |
form | object | An object representing a form, such as a customs form or scan form. This property is not required. If it is provided, it must contain all of its required properties, listed below. | |
form.name | string | The user-friendly name of the form (e.g. "Customs Form", "Scan Form"). This string must not contain newline characters. | |
form.type | string | ✔ | The type of form. This value should be |
form.size | string | ✔ | The size of the form. Valid values include the following:
|
form.format | string | ✔ | The file format of the form. Valid values include the following:
|
form.data | ✔ | The form data, in the specified file format. | |
trackingNumber | string | The master tracking number for the entire shipment. For single-piece shipments, this will be the same as the package tracking number. For multi-piece shipments, this may be a separate tracking number, or the same tracking number as one of the packages. This string must not contain newline characters. | |
identifiers | Your own identifiers for this shipment. | ||
deliveryDateTime | date/time object, | The estimated date and time the shipment will be delivered. | |
packages | object[] | An array of objects containing confirmation details about each package in the shipment. This array will contain at least one item. | |
packages[].trackingNumber | string | The master tracking number for the entire shipment. For single-piece shipments, this will be the same as the package tracking number. For multi-piece shipments, this may be a separate tracking number, or the same tracking number as one of the packages. This string must not contain newline characters. | |
packages[].identifiers | Your own identifiers for this package. | ||
packages[].metadata | object | Custom data about this package that will be persisted by ShipEngine Connect. Must be JSON serializable. |
Example
module.exports = async function createShipment(transaction, shipment) { // STEP 1: Validation for (let parcel of shipment.packages) { if (parcel.packaging.id === OWN_PACKAGING && parcel.weight.grams > 100000) { throw new Error(`${parcel.packaging.name} cannot weigh more than 100 kilograms`); } } // STEP 2: Create the data that the carrier's API expects let data = { operation: "generate_label", session_id: transaction.session.id, service_code: shipment.deliveryService.code, confirmation_code: shipment.deliveryConfirmation.code, ship_date: shipment.shipDateTime.toISOString(), from_zone: parseInt(shipment.shipFrom.postalCode, 10), to_zone: parseInt(shipment.shipTo.postalCode, 10), total_weight: shipment.package.weight.ounces, }; // STEP 3: Call the carrier's API const response = await apiClient.request({ data }); // STEP 4: Create the output data that ShipEngine expects return await formatShipment(response.data);}
import { ChargeType, DocumentFormat, DocumentSize, DocumentType, NewShipment, ShipmentConfirmation, Transaction} from "@shipengine/connect";export default async function createShipment( transaction: Transaction<Session>, shipment: NewShipment): Promise<ShipmentConfirmation> { // STEP 1: Validation for (let parcel of shipment.packages) { if (parcel.packaging.id === box.id && parcel.weight.ounces > (150 * 16)) { throw new Error(`${parcel.packaging.name} cannot weigh more than 150 pounds`); } else if (parcel.packaging.id === bag.id && parcel.weight.ounces > (45 * 16)) { throw new Error(`${parcel.packaging.name} cannot weigh more than 45 pounds`); } } // STEP 2: Create the data that the carrier's API expects let data: GenerateLabelRequest = { operation: "generate_label", session_id: transaction.session.id, service_code: shipment.deliveryService.code, confirmation_code: shipment.deliveryConfirmation.code, ship_date: shipment.shipDateTime.toISOString(), from_zone: parseInt(shipment.shipFrom.postalCode, 10), to_zone: parseInt(shipment.shipTo.postalCode, 10), total_weight: shipment.package.weight.ounces, }; // STEP 3: Call the carrier's API const response = await apiClient.request<GenerateLabelResponse>({ data }); // STEP 4: Create the output data that ShipEngine expects return formatShipment(response.data);}