create-draft-order
Generate draft orders in Shopify by specifying line items, customer email, and shipping details. Streamline order creation with structured inputs for accuracy and efficiency.
Instructions
Create a draft order
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| Yes | Customer email | ||
| lineItems | Yes | Line items to add to the order | |
| note | No | Optional note for the order | |
| shippingAddress | Yes | Shipping address details |
Implementation Reference
- src/index.ts:432-482 (registration)Registration of the 'create-draft-order' MCP tool, including Zod input schema and thin wrapper handler delegating to ShopifyClient.createDraftOrderserver.tool( "create-draft-order", "Create a draft order", { lineItems: z .array( z.object({ variantId: z.string(), quantity: z.number(), }) ) .describe("Line items to add to the order"), email: z.string().email().describe("Customer email"), shippingAddress: z .object({ address1: z.string(), city: z.string(), province: z.string(), country: z.string(), zip: z.string(), firstName: z.string(), lastName: z.string(), countryCode: z.string(), }) .describe("Shipping address details"), note: z.string().optional().describe("Optional note for the order"), }, async ({ lineItems, email, shippingAddress, note }) => { const client = new ShopifyClient(); try { const draftOrderData: CreateDraftOrderPayload = { lineItems, email, shippingAddress, billingAddress: shippingAddress, // Using same address for billing tags: "draft", note: note || "", }; const draftOrder = await client.createDraftOrder( SHOPIFY_ACCESS_TOKEN, MYSHOPIFY_DOMAIN, draftOrderData ); return { content: [{ type: "text", text: JSON.stringify(draftOrder, null, 2) }], }; } catch (error) { return handleError("Failed to create draft order", error); } } );
- Core implementation of draft order creation: executes GraphQL mutation draftOrderCreate against Shopify Admin API using shopifyGraphqlRequest, handles user errors, returns draft order ID and name.async createDraftOrder( accessToken: string, myshopifyDomain: string, draftOrderData: CreateDraftOrderPayload ): Promise<DraftOrderResponse> { const graphqlQuery = gql` mutation draftOrderCreate($input: DraftOrderInput!) { draftOrderCreate(input: $input) { draftOrder { id name } userErrors { field message } } } `; const res = await this.shopifyGraphqlRequest<{ data: { draftOrderCreate: { draftOrder: { id: string; name: string; }; userErrors: Array<{ field: string[]; message: string; }>; }; }; }>({ url: `https://${myshopifyDomain}/admin/api/${this.SHOPIFY_API_VERSION}/graphql.json`, accessToken, query: graphqlQuery, variables: { input: draftOrderData, }, }); const draftOrder = res.data.data.draftOrderCreate.draftOrder; const userErrors = res.data.data.draftOrderCreate.userErrors; if (userErrors.length > 0) { throw getGraphqlShopifyUserError(userErrors, { myshopifyDomain, draftOrderData, }); } return { draftOrderId: draftOrder.id, draftOrderName: draftOrder.name, }; }
- TypeScript type definitions for CreateDraftOrderPayload (input) and DraftOrderResponse (output) used by the createDraftOrder handler.export type CreateDraftOrderPayload = { lineItems: Array<{ variantId: string; quantity: number; appliedDiscount?: { title: string; value: number; valueType: "FIXED_AMOUNT" | "PERCENTAGE"; }; }>; shippingAddress: { address1: string; address2?: string; countryCode: string; firstName: string; lastName: string; zip: string; city: string; country: string; province?: string; provinceCode?: string; phone?: string; }; billingAddress: { address1: string; address2?: string; countryCode: string; firstName: string; lastName: string; zip: string; city: string; country: string; province?: string; provinceCode?: string; phone?: string; }; email: string; tags: string; note: string; }; export type DraftOrderResponse = { draftOrderId: string; draftOrderName: string; };