create-draft-order
Generate draft orders in Shopify by specifying line items, customer email, and shipping address for order preparation and processing.
Instructions
Create a draft order
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| lineItems | Yes | Line items to add to the order | |
| Yes | Customer email | ||
| shippingAddress | Yes | Shipping address details | |
| note | No | Optional note for the order |
Implementation Reference
- src/index.ts:404-454 (handler)MCP tool handler and registration for 'create-draft-order'. Defines input schema with Zod, constructs CreateDraftOrderPayload, instantiates ShopifyClient, calls createDraftOrder method, and returns JSON response or error.server.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 in ShopifyClient class. Executes GraphQL mutation 'draftOrderCreate' via shopifyGraphqlRequest, handles userErrors, returns DraftOrderResponse.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, }; }
- src/index.ts:407-430 (schema)Zod schema defining input parameters for the create-draft-order tool: lineItems array, email, shippingAddress object, optional note.{ 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"), },
- TypeScript type definition for CreateDraftOrderPayload used in the tool's handler and ShopifyClient method.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; };
- TypeScript type for the response returned by createDraftOrder.export type DraftOrderResponse = { draftOrderId: string; draftOrderName: string;