Jeelel Posted October 19 Posted October 19 Hello Loyverse Community, I am trying to create a new receipt using the v1.0 API from a Node.js application, but I am stuck on a persistent 400 Bad Request error. The API response is always the same: { "errors": [ { "code": "MISSING_REQUIRED_PARAMETER", "details": "Field must be set", "field": "object.store_id" } ] } The strange thing is that I am 100% sure the store_id is present in my request payload. I have even hardcoded the values directly into my code to ensure there are no issues with environment variables, but the error persists. Here is a simplified version of my Node.js code using fetch: // My credentials are hardcoded for this test const LOYVERSE_API_TOKEN = "77e5158ae30642a295f1c7a6a0e057c6"; // This is a valid token const STORE_ID = "d88730d4-82ef-4585-844a-c665ca2b3f2a"; // This is my correct Store ID const EMPLOYEE_ID = "d88730d4-82ef-4585-844a-c665ca2b3f2a"; // I'm using the Store ID as Employee ID // The payload I'm sending const payload = { receipts: [{ receipt_date: new Date().toISOString(), store_id: STORE_ID, // As you can see, the store_id is set here employee_id: EMPLOYEE_ID, receipt_type: 'SALE', total_money: 17000, line_items: [{ variant_id: "b0fe8eb7-9028-4608-8774-c7e6014e478a", // Example: Hawaiana Grande quantity: 1, price: 17000 }], payments: [{ payment_type_id: "ba69d7c5-cce1-4192-89d3-0d8e92288ed9", // Cash amount: 17000, paid_at: new Date().toISOString() }] }] }; // The API call fetch('https://api.loyverse.com/v1.0/receipts', { method: 'POST', headers: { 'Authorization': `Bearer ${LOYVERSE_API_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }) .then(res => res.json()) .then(json => console.log(json)); Things I have already tried: Confirmed my STORE_ID is correct by checking the URL in my Back Office. Generated brand new Personal Access Tokens. I know the token is authenticating correctly because a GET request to /receipts with cURL returns a 402 PAYMENT_REQUIRED error, not a 401 UNAUTHORIZED error. Hardcoded all credentials to rule out any .env file reading issues. Checked my code files for any invisible characters or encoding problems. My question is: Why would the API claim store_id is missing when it is clearly present and hardcoded in the JSON payload? Could this be a known issue or something specific to my account configuration? Thank you in advance for any help or ideas.
Yasuaki Posted Thursday at 11:45 AM Posted Thursday at 11:45 AM Hello. Thank you for your question. This problem is occurring because the API endpoint and the structure of the JSON payload you are sending do not match. The API is reporting that object.store_id is missing because it expects to find the store_id field at the outermost (top) level of the JSON you sent. Currently, your store_id is inside an array named receipts, which is why the API cannot find it. Cause of the Problem The URL you are sending to is https://api.loyverse.com/v1.0/receipts. This endpoint is for creating a single receipt and expects a single receipt object as the request body (e.g., { "store_id": "...", "receipt_date": "...", ... }). However, the payload you are sending is in the format {"receipts": [ ... ]}. This is the format for the batch endpoint, which is used to create multiple receipts at once. Solution Continue using the POST /v1.0/receipts endpoint, but remove the receipts array wrapper from your payload. Send the single receipt object directly.
Recommended Posts