Elements SDK
The Elements SDK is a JavaScript SDK for implementing ShipEngine Elements. You can install it via NPM or access it through our CDN.
Quick Start
Install Peer Dependencies:
Elements CDN
The ShipEngine Elements SDK can be included in any page by loading the bundle directly from the Elements CDN. If desired, you may specify a particular version of the SDK by using the following filename format: https://cdn.shipengine.com/shipengine-elements-sdk.<major_version>.<minor-version>.<patch-version>.mjs
. You may omit any level of version number to always load the latest at that level. For example, shipengine-elements-sdk.1.2.34.mjs
will pull version 1.2.34, while shipengine-elements-sdk.1.mjs
will pull the latest minor and patch version for major version 1.
Initialize Elements
You'll need to use the Elements SDK class constructor to initialize Elements. This constructor has three arguments:
Token Callback Function (required): This function, a key component of the Elements SDK, fetches and refreshes the tokens used by Elements. Check out the Elements Getting Started guide for more detailed information about generating Elements tokens.
Initialization Parameters (optional): This object allows you to customize various aspects of the Elements SDK. It's optional; See the initialization parameters section
Container (optional): An HTML ID or HTML element that the Elements React root and CSS reset will be mounted to. The SDK uses a shadow root to isolate its CSS styles from the rest of your application, preventing conflicts and ensuring consistent styling. If you provide a container, the SDK will create a shadow root on that container and render the elements within it. If you don't provide a container, the SDK will create a new container element with a shadow root and append it to the document body.
Initialization Parameters
Args/Props | Description |
---|---|
baseURL | string, optional A fully qualified domain name used as the base URL to route all API calls from the Elements. This is defaulted to https://elements.shipengine.com |
defaultQueryClientOptions | object, optional Initialization options of the ReactQuery client. See the QueryClient Docs for more info. |
emotionCacheShadowRootContainer | ShadowRoot, optional Deprecated A reference to a shadow root used for isolating the Elements style sheets, CSS resets, and emotion.js cache. |
features | object, optional A feature flag configuration object for turning features on and off. See Configuring Elements Features for a comprehensive list of features. |
headers | object, optional Optional HTTP headers to be sent with all requests. |
locale | string, optional Defaults to en-us |
onApiError | object, optional Callback function that will be executed whenever there is an error within the API. |
onError | function, optional An optional callback function to be invoked when an error occurs during a request to the API. |
scope | string, optional Element id to apply the theme, font family, and CSS reset. By default, the font will be applied to the body element, and the reset will be applied to the root HTML element.configured. |
themeConfig | object, optional The theme configuration object. Check out the theming documentation to learn more about theming your Elements integration. |
Configuring Elements Features
Global Features
The following properties are configured within the globalFeatures
property.
Params | Description |
---|---|
enabledExternalCarriers | string[], optional List external carrier codes for carriers that can be registered through the Connect External Carrier Element. If this is omitted, external carriers cannot be registered. See the carriers section in the getting started guide for more information. IMPORTANT: If you exclude this property, or set the value to an empty array without including any supported carrier codes, users will not be able to view and connect External Carriers in the Account Settings Element or the Manage External Carriers Element |
enabledShipEngineCarriers | string[], optional List of ShipEngine carrier codes for carriers that can be registered through the Onboarding Element. See the carriers section in the getting started guide for more information. IMPORTANT: If you exclude this property, or set the value to an empty array without including any supported carrier codes, users will encounter an error that will prevent them from advancing through the onboarding process within the Onboarding Element and will not be able to access the Carrier Services Element. |
poweredByShipEngine | boolean, optional Enables the Powered by ShipEngine logo in the footer various elements. |
Global features are set within the features parameter of the Elements SDK class constructor and cannot be overridden within Element specific feature props.
Element specific Features
We recommend configuring the features you want to use in the Elements Provider or the SDK constructor for the Element. However, if you need to override features for a specific Element, you can use the feature prop for that Element, provided the Element supports it.
Params | Description |
---|---|
purchaseLabelFeatures | string[], optional See Purchase Label Features for a comprehensive list. |
shipmentSummaryFeatures | string[], optional See Shipment Summary Features for a comprehensive list. |
accountSettingsFeatures | string[], optional See Account Settings Features for a comprehensive list. |
labelsGridFeatures | string[], optional See Labels Grid Features for a comprehensive list. |
Rendering Elements
elements.create(element)
Params | Description |
---|---|
element | string, required The name of the Element to be created. |
accountSettings
carrierServices
connectExternalCarrier
labelLayout
labelWorkFlow
manageCarriers
manageExternalCarriers
manageFunding
manageWarehouses
onboarding
paymentMethodSettings
purchaseLabel
shipmentSummary
transactionHistory
unitSettings
voidLabel
labelsGrid
elements.destroy()
Removes Elements from the DOM and destroys it. A destroyed Element instance can not be re-activated or re-mounted to the DOM.
elements.getClient()
Returns an instance of the ShipEngine Elements API JavaScript client. This client can be used to interact directly with the ShipEngine API.
If no parameters are passed to getClient()
, the client will be configured based off of the initialization parameters passed to the Elements SDK constructor. If desired, you may pass a token and configuration object to the getClient()
method to specify a different configuration:
Args/Props | Description |
---|---|
getToken | function, required If defining your own API client configuration, the token callback function passed to the Elements SDK constructor must be included. |
baseURL | string, optional A fully qualified domain name used as the base URL to route all API calls from the client. |
headers | object, optional Optional HTTP headers to be sent with all requests. |
onApiError | object, optional Callback function that will be executed whenever there is an error within the API. |
Side Panel Methods (deprecated)
While working with the Elements SDK, you probably noticed methods for rendering individual Elements in a side panel. Methods like onboardUser
or purchaseLabel
, accessible via the Elements SDK class, have been deprecated in Elements v2 and will be removed in future versions.
Element Render Methods
There are two ways to render Elements in your application:
- Mounting elements in a specified DOM node or,
- Opening the Elements in a pre-configured slide in drawer (Side Panel).
The Elements SDK allows you to place Elements in any container in you app, but the Elements were designed to look best at a view port width of 440 to 800 pixels
.
element.mount(domElement, props)
Before you mount an Element, you must create a container DOM element. The mount
method attaches your Element to the DOM. mount
accepts either an ID Attribute (e.g., purchaseLabelId
) or a DOM element as well as the Elements props. See the documentation for that Element for more information on its props.
You may encounter an issue where the container DOM element wrapping a ShipEngine Element does not automatically resize to fit the component's content. This can lead to layout problems, particularly when working in plain HTML, CSS, and JavaScript environments.
To resolve this, we recommend applying specific CSS styles to the wrapper element where the Element is rendered:
If you're using a framework like React, Vue, or Angular, you are unlikely to encounter this issue, as these frameworks typically handle dynamic DOM sizing automatically. However, this CSS solution is particularly useful when working in environments that rely solely on raw HTML, CSS, and JavaScript
Params | Description |
---|---|
domElement | string/DOM ELEMENT, required The html ID or DOM element where your Element will be mounted. |
props | object, optional Props for the Element being mounted. |
element.update(props)
Updates the props for the element
Params | Description |
---|---|
props | object, optional Props for the Element being updated. |
element.unmount()
Unmounts the Element from the DOM. Call the element.mount()
method to re-attach it to the DOM.
element.renderSidePanel(props)
Renders the Element inside a side panel. The method accepts the Elements props as well as the props for the side panel that it is rendered in side of.
props | Description |
---|---|
elementProps | object, optional Props for the Element being updated. |
sidePanelHeaderProps | object, optional Props for the Element being updated. |
sidePanelProps | object, optional Props for the Element being updated. |
Args/Props | Description |
---|---|
header | ReactNode, optional Component to use as the header of the component |
footer | ReactNode, optional Component to use as the footer of the component |
position | string, optional Value indicating in which side of the screen the component should be placed. Either right or left |
size | string, optional Value indicating a predefined size. Either regular or large |
containerId | string, optional Id of the container from which the SidePanel will get the height and top |
onBackdropClick | function, optional An optional async/sync callback to be executed when click is performed on the outside of the side panel |
applyBackdropToBody | boolean, optional Flag to indicate if the backdrop should be placed inside the body |
Args/Props | Description |
---|---|
title | string, required Side panel title |
closeButtonTooltip | string, optional Text rendered inside the side panel close button tool tip. |
closeAriaLabel | string, optional Aria label for the close button |
onClickClose | function, optional Optional callback function invoked when the side panel close button is clicked |
element.closeSidePanel()
Closes the currently opened side panel.
Creating Shipments
If you wish to purchase a label using pre-populated values in the Purchaselabel forms, the recommended way to define the shipment values is to create a new shipment using the ShipEngine API, and then pass the new Shipment ID to the PurchaseLabel or LabelWorkflow Element.
The Elements SDK provides an API client which can be used to generate the shipment using the same authentication token used by Elements. See the example below:
Only the ship_to
and ship_from
properties are required; The rest may be left blank to be filled in by the user in the Element. The packages.products
array is used for Customs information, and is not generally required for local shipments.
For more information on the available fields within the Shipment object, see the ShipEngine API documentation. For additional functionality provided by the Elements API Client, see the ShipEngine Elements API Clients documentation.