use Api()React Hook
Returns the full API object that was passed in to your extension when it was created. Depending on the extension target, this object can contain different properties.
For example, the purchase.checkout.cart-line-item.render-after
extension target will return the CartLineDetailsApi object.
Other targets may only have access to the StandardApi object,
which contains a basic set of properties about the checkout.
For a full list of the API available to each extension target, see the ExtensionTargets type.
Anchor to useApi-parametersParameters
- Anchor to _target_targetTarget
Anchor to useApi-returnsReturns
UseApiGeneratedType
Returns the full API object that was passed in to your extension when it was created. Depending on the extension target, this object can contain different properties. For example, the `purchase.checkout.cart-line-item.render-after` extension target will return the [CartLineDetailsApi](/docs/api/checkout-ui-extensions/apis/cartlinedetailsapi) object. Other targets may only have access to the [StandardApi](/docs/api/checkout-ui-extensions/apis/standardapi) object, which contains a basic set of properties about the checkout. For a full list of the API available to each extension target, see the [ExtensionTargets type](/docs/api/checkout-ui-extensions/apis/extensiontargets).
- _target
Target
ApiForRenderExtension<Target>
export function useApi<
Target extends RenderExtensionTarget = RenderExtensionTarget,
>(_target?: Target): ApiForRenderExtension<Target> {
const api = useContext(ExtensionApiContext);
if (api == null) {
throw new CheckoutUIExtensionError(
'You can only call this hook when running as a checkout UI extension.',
);
}
return api as ApiForRenderExtension<Target>;
}
ApiForRenderExtension
For a given rendering extension target, returns the type of the API that the extension will receive at runtime. This API type is the second argument to the callback for that extension target. The first callback for all of the rendering extension targets each receive a `RemoteRoot` object.
ApiForRenderExtension<Target>
Accessing Properties
React
examples
Accessing Properties
description
The extension API is passed as a parameter to the extension target function. In React, you can access it from any component through the `useApi()` hook.
React
import { reactExtension, Text, useApi, } from '@shopify/ui-extensions-react/checkout'; export default reactExtension( 'purchase.checkout.block.render', () => <Extension />, ); function Extension() { const {shop} = useApi(); return <Text>Shop name: {shop.name}</Text>; }