Getting 400 error creating draft order

when creating draft order with graphql with the following function 400 error encountered

async function createBulkOrder(
  locationId,
  locationName,
  monthName,
  year
) {
  const lineItems = [
    {
      variantId: "gid://shopify/ProductVariant/42726908887213",
      quantity: 2,
    },
    {
      variantId: "gid://shopify/ProductVariant/42726908919981",
      quantity: 3,
    },
    {
      variantId: "gid://shopify/ProductVariant/43314902728877",
      quantity: 4,
    },
  ];

  if (!lineItems.length) {
    console.error("Error: lineItems array is empty or missing");
    return;
  }

  const createdVariables = {
    input: {
      lineItems: lineItems,
      tags: ["barcelona-warehouse", `auto-generated`, `$jan-2025`],
      note: `Auto-generated draft order at ${locationName}. Month: ${monthName} ${year}. Location ID: ${locationId}`,
    },
  };
  console.log(
    "Variables being sent:",
    JSON.stringify(createdVariables, null, 2)
  );
  const mutation = `mutation draftOrderCreate($input: DraftOrderInput!) {
    draftOrderCreate(input: $input) {
      draftOrder {
        id
        name
        lineItems(first: 150) { 
          nodes {
            id
            quantity
            variant {
              id
            }
          }
        }    
      }
      userErrors {
        field
        message
      }  
    }
  }`;
  try {
    const response = await shopify.graphql({
      query: mutation,
      variables: createdVariables,
    });
  
    console.log("Full GraphQL Response:", JSON.stringify(response, null, 2));
  
    // Rest of the success handling logic
    // ...
  } catch (error) {
    console.error("Error creating order:", error.message);
    console.error("Error stack trace:", error.stack);
    
    // Extract any available error information
    if (error.body) {
      console.error("Error body:", typeof error.body === 'string' ? error.body : JSON.stringify(error.body, null, 2));
    }
    
    if (error.statusCode) {
      console.error("Error status code:", error.statusCode);
    }
    
    if (error.statusMessage) {
      console.error("Error status message:", error.statusMessage);
    }
    
    if (error.headers) {
      console.error("Error headers:", error.headers);
    }
    
    // If there are GraphQL specific errors
    if (error.graphQLErrors) {
      console.error("GraphQL Errors:", JSON.stringify(error.graphQLErrors, null, 2));
    }
    
    // If there are network errors
    if (error.networkError) {
      console.error("Network Error:", error.networkError);
    }
    
    // Log the entire error object for inspection
    console.error("Full error object keys:", Object.keys(error));
    
    throw error; // Rethrow to handle it in higher-level code
  }
}

this is the error logs

Error creating order: Response code 400 (Bad Request)
Error stack trace: HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:118:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Full error object keys: [ 'name', 'code', 'timings' ]
node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^

HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:118:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1741396317990,
    socket: 1741396317991,
    lookup: 1741396317998,
    connect: 1741396318009,
    secureConnect: 1741396318027,
    upload: 1741396318027,
    response: 1741396318490,
    end: 1741396318496,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 1,
      dns: 7,
      tcp: 11,
      tls: 18,
      request: 0,
      firstByte: 463,
      download: 6,
      total: 506
    }
  }
}

i am using shopify-api-node package and api version is 2024-10
please help

"tags": [
      "barcelona-warehouse", 
       `auto-generated`,
       `$jan-2025`
    ],

I think the symbol causing the issue. If you are using, assign the value to a variable and use the variable inside the createdVariables variable.

removed the $ symbol but encounter same issue.

Sorry about missing the symbol in my last comment. The actual issue is the use of backtick around “auto-generated”`. It should be enclosed in double quotes instead.

Here’s the corrected version for rechecking:

"tags": [
    "barcelona-warehouse",
    "auto-generated",
    "$jan-2025"
]

Also from note field. First assign note to a variable and the use it in the input.

tried removing backtick but same 400 issue.

Could you please share the input request(createdVariables)?

sure.
here it is

      query: mutation,
      variables: {
        input: {
          lineItems: [
            {
              variantId: "gid://shopify/ProductVariant/42726908887213",
              quantity: 2,
            },
            {
              variantId: "gid://shopify/ProductVariant/42726908919981",
              quantity: 3,
            },
            {
              variantId: "gid://shopify/ProductVariant/43314902728877",
              quantity: 4,
            },
          ],
          tags: ["barcelona-warehouse", "auto-generated", "feb-2025"],
          note: `Auto-generated draft order at ${locationName}. Month: ${monthName} ${year}. Location ID: ${locationId}`,
        },
      },
    });```

Could you please try the request without note?

run and encountered same issue

Error creating order: Response code 400 (Bad Request)
Error stack trace: HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:118:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Full error object keys: [ 'name', 'code', 'timings' ]
node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^

HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:118:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1741512758358,
    socket: 1741512758360,
    lookup: 1741512758372,
    connect: 1741512758386,
    secureConnect: 1741512758404,
    upload: 1741512758404,
    response: 1741512758877,
    end: 1741512758884,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 2,
      dns: 12,
      tcp: 14,
      tls: 18,
      request: 0,
      firstByte: 473,
      download: 7,
      total: 526
    }
  }
}

Could you please try the below request? I have removed unwanted comma from the input. If not work also try without note. None of them worked please give the latest request sample you tried.

{
        input: {
          lineItems: [
            {
              variantId: "gid://shopify/ProductVariant/42726908887213",
              quantity: 2,
            },
            {
              variantId: "gid://shopify/ProductVariant/42726908919981",
              quantity: 3,
            },
            {
              variantId: "gid://shopify/ProductVariant/43314902728877",
              quantity: 4,
            }
          ],
          tags: ["barcelona-warehouse", "auto-generated", "feb-2025"],
          note: `Auto-generated draft order at ${locationName}. Month: ${monthName} ${year}. Location ID: ${locationId}`,
        }
      }
    }

same error with the following request

    const response = await shopify.graphql({
      query: mutation,
      variables: {
        input: {
          lineItems: [
            {
              variantId: "gid://shopify/ProductVariant/42726908887213",
              quantity: 2,
            },
            {
              variantId: "gid://shopify/ProductVariant/42726908919981",
              quantity: 3,
            },
            {
              variantId: "gid://shopify/ProductVariant/43314902728877",
              quantity: 4,
            }
          ],
          tags: ["barcelona-warehouse", "auto-generated", "feb-2025"]
        }
      }
    });

i am using shopify-api-node npm package

@Roman_Khan_Rajib Please updated the code as below:

 const response = await admin.graphql(mutation, {
      variables: createdVariables,
    });

thank you for helping me.
but this time input get null as value following your instruction

  const mutation = `mutation draftOrderCreate($input: DraftOrderInput!) {
    draftOrderCreate(input: $input) {
      draftOrder {
        id
        name
        lineItems(first: 150) { 
          nodes {
            id
            quantity
            variant {
              id
            }
          }
        }    
      }
      userErrors {
        field
        message
      }  
    }
  }`;

    const response = await shopify.graphql(mutation, {
      variables: {
        input: {
          lineItems: [
            {
              variantId: "gid://shopify/ProductVariant/42726908887213",
              quantity: 2,
            },
            {
              variantId: "gid://shopify/ProductVariant/42726908919981",
              quantity: 3,
            },
            {
              variantId: "gid://shopify/ProductVariant/43314902728877",
              quantity: 4,
            },
          ],
          tags: ["barcelona-warehouse", "auto-generated", "feb-2025"],
          note: `Auto-generated draft order at barcelona-warehouse. Month: feb 2025. Location ID: 72361017517`,
        },
      },
    });

and encounter invalid value for input


Error creating order: Variable $input of type DraftOrderInput! was provided invalid value
Error stack trace: RequestError: Variable $input of type DraftOrderInput! was provided invalid value
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:113:42)
    at afterResponse (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/shopify-api-node/index.js:297:15)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:87:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Full error object keys: [ 'name', 'code', 'timings', 'locations', 'path', 'extensions' ]
node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^

RequestError: Variable $input of type DraftOrderInput! was provided invalid value
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:113:42)
    at afterResponse (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/shopify-api-node/index.js:297:15)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:87:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_GOT_REQUEST_ERROR',
  timings: {
    start: 1741518570003,
    socket: 1741518570004,
    lookup: 1741518570024,
    connect: 1741518570037,
    secureConnect: 1741518570058,
    upload: 1741518570058,
    response: 1741518570724,
    end: 1741518570729,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 1,
      dns: 20,
      tcp: 13,
      tls: 21,
      request: 0,
      firstByte: 666,
      download: 5,
      total: 726
    }
  },
  locations: [ { line: 1, column: 27 } ],
  path: undefined,
  extensions: {
    value: null,
    problems: [ { path: [], explanation: 'Expected value to not be null' } ]
  }
}

I have used the below code and successfully created the draft order.

const lineItems = [
    {
      variantId: 'gid://shopify/ProductVariant/123456',
      quantity: 2,
    },
    {
      variantId: 'gid://shopify/ProductVariant/123456',
      quantity: 3,
    },
    {
      variantId: 'gid://shopify/ProductVariant/123456',
      quantity: 4,
    },
  ];

  if (!lineItems.length) {
    console.error('Error: lineItems array is empty or missing');
    return;
  }
  const locationName = 'test';
  const locationId = 'gid://shopify/Location/123456789';
  const monthName = 'January';
  const year = 2025;

  const createdVariables = {
    input: {
      lineItems: lineItems,
      tags: ['barcelona-warehouse', `auto-generated`, `$jan-2025`],
      note: `Auto-generated draft order at ${locationName}. Month: ${monthName} ${year}. Location ID: ${locationId}`,
    },
  };
  console.log(
    'Variables being sent:',
    JSON.stringify(createdVariables, null, 2),
  );
  const mutation = `mutation draftOrderCreate($input: DraftOrderInput!) {
    draftOrderCreate(input: $input) {
      draftOrder {
        id
        name
        lineItems(first: 150) { 
          nodes {
            id
            quantity
            variant {
              id
            }
          }
        }    
      }
      userErrors {
        field
        message
      }  
    }
  }`;
  try {
    const response = await admin.graphql(mutation, {
      variables: createdVariables,
    });

    console.log('Full GraphQlL Response:', JSON.stringify(response, null, 2));

    // Rest of the success handling logic
    // ...
  } catch (error) {
    console.error('Error creating order:', error);
    console.error('Error stack trace:', error.stack);
  }

Please note:

const locationName = ‘test’;
const locationId = ‘gid://shopify/Location/123456789’;
const monthName = ‘January’;
const year = 2025;

I have added the above variables and variantids for testing. So please remove.

which api version you used???

Lastest stable version. Could you please try with some hardcoded values?

Or

Please provide the code.

i have used this hardcoded values

    const response = await shopify.graphql(mutation, {
      variables: {
        input: {
          lineItems: [
            {
              variantId: "gid://shopify/ProductVariant/42726908887213",
              quantity: 2,
            },
            {
              variantId: "gid://shopify/ProductVariant/42726908919981",
              quantity: 3,
            },
            {
              variantId: "gid://shopify/ProductVariant/43314902728877",
              quantity: 4,
            }
          ],
          tags: ["barcelona-warehouse", "auto-generated", "feb-2025"],
          note: `Auto-generated draft order at barcelona-warehouse. Month: feb 2025. Location ID: 72361017517`,
        }
      }
    });

and encounter the following error

Error creating order: Variable $input of type DraftOrderInput! was provided invalid value
Error stack trace: RequestError: Variable $input of type DraftOrderInput! was provided invalid value
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:113:42)
    at afterResponse (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/shopify-api-node/index.js:297:15)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:87:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Full error object keys: [ 'name', 'code', 'timings', 'locations', 'path', 'extensions' ]
node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^

RequestError: Variable $input of type DraftOrderInput! was provided invalid value
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:113:42)
    at afterResponse (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/shopify-api-node/index.js:297:15)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:87:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_GOT_REQUEST_ERROR',
  timings: {
    start: 1741656814402,
    socket: 1741656814404,
    lookup: 1741656814421,
    connect: 1741656814436,
    secureConnect: 1741656814453,
    upload: 1741656814454,
    response: 1741656815027,
    end: 1741656815033,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 2,
      dns: 17,
      tcp: 15,
      tls: 17,
      request: 1,
      firstByte: 573,
      download: 6,
      total: 631
    }
  },
  locations: [ { line: 1, column: 27 } ],
  path: undefined,
  extensions: {
    value: null,
    problems: [ { path: [], explanation: 'Expected value to not be null' } ]
  }
}

Hi @Roman_Khan_Rajib

Before calling Shopify API, print out the parameters.

code executed as follows:

 const mutation = `mutation draftOrderCreate($input: DraftOrderInput!) {
    draftOrderCreate(input: $input) {
      draftOrder {
        id
        name
        lineItems(first: 150) { 
          nodes {
            id
            quantity
            variant {
              id
            }
          }
        }    
      }
      userErrors {
        field
        message
      }  
    }
  }`;
  const createdVariables = {
    input: {
      lineItems: [
        {
          variantId: "gid://shopify/ProductVariant/42726908887213",
          quantity: 2,
        },
        {
          variantId: "gid://shopify/ProductVariant/42726908919981",
          quantity: 3,
        },
        {
          variantId: "gid://shopify/ProductVariant/43314902728877",
          quantity: 4,
        },
      ],
      tags: ["barcelona-warehouse", "auto-generated", "feb-2025"],
      note: `Auto-generated draft order at barcelona-warehouse. Month: feb 2025. Location ID: 72361017517`,
    },
  };
  console.log(
    "Variables being sent:",
    JSON.stringify(createdVariables, null, 2),
    JSON.stringify(mutation, null, 2)
  );
  try {
    const response = await shopify.graphql(mutation, {
      variables: createdVariables,
    });

printed parameters and error as follows:

Variables being sent: {
  "input": {
    "lineItems": [
      {
        "variantId": "gid://shopify/ProductVariant/42726908887213",
        "quantity": 2
      },
      {
        "variantId": "gid://shopify/ProductVariant/42726908919981",
        "quantity": 3
      },
      {
        "variantId": "gid://shopify/ProductVariant/43314902728877",
        "quantity": 4
      }
    ],
    "tags": [
      "barcelona-warehouse",
      "auto-generated",
      "feb-2025"
    ],
    "note": "Auto-generated draft order at barcelona-warehouse. Month: feb 2025. Location ID: 72361017517"
  }
} "mutation draftOrderCreate($input: DraftOrderInput!) {\n    draftOrderCreate(input: $input) {\n      draftOrder {\n        id\n        name\n        lineItems(first: 150) { \n          nodes {\n            id\n            quantity\n            variant {\n              id\n            }\n          }\n        }    \n      }\n      userErrors {\n        field\n        message\n      }  \n    }\n  }"
Error creating order: Variable $input of type DraftOrderInput! was provided invalid value
Error stack trace: RequestError: Variable $input of type DraftOrderInput! was provided invalid value
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:113:42)
    at afterResponse (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/shopify-api-node/index.js:297:15)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:87:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Full error object keys: [ 'name', 'code', 'timings', 'locations', 'path', 'extensions' ]
node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^

RequestError: Variable $input of type DraftOrderInput! was provided invalid value
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:113:42)
    at afterResponse (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/shopify-api-node/index.js:297:15)
    at Request.<anonymous> (/Users/rajib/Desktop/Project/Web/clients/linda/linda-shopify-excel-integration-12-02-25/node_modules/got/dist/source/as-promise/index.js:87:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_GOT_REQUEST_ERROR',
  timings: {
    start: 1741665958671,
    socket: 1741665958673,
    lookup: 1741665958719,
    connect: 1741665958730,
    secureConnect: 1741665958751,
    upload: 1741665958751,
    response: 1741665959519,
    end: 1741665959526,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 2,
      dns: 46,
      tcp: 11,
      tls: 21,
      request: 0,
      firstByte: 768,
      download: 7,
      total: 855
    }
  },
  locations: [ { line: 1, column: 27 } ],
  path: undefined,
  extensions: {
    value: null,
    problems: [ { path: [], explanation: 'Expected value to not be null' } ]
  }