Unable to add document to invoice via API

Hello! I’m struggling to add a document to an existing invoice via the /api/v1/invoices/{id}/upload endpoint.

I’ve hit a number of confusing error messages, but my current block is this error:

No query results for model [App\\Models\\Invoice].

I’m currently using a JavaScript framework that handles the post for me, but I have been able to successfully use the same wrapper class for many other endpoints.

My approximate code is:

  const testFileBlob = Utilities.newBlob('Upload me!', 'text/plain', 'upload-test.txt');
  const options = {
    method: 'post',
    muteHttpExceptions: true,
    headers: {
      'Content-Type': 'multipart/form-data',
      "X-API-TOKEN": "[TOKEN]",
      "X-Requested-With": "XMLHttpRequest",
    },
    payload: {
      _method: "PUT",
      // "documents[]": testFileBlob,
      // "documents[0]": testFileBlob,
      documents: [testFileBlob],
    }
  };
  const documentUploadUrl = INVOICE_NINJA_API_PATH + "/invoices/[invoice_id]/upload";
  const documentUploadResponse = UrlFetchApp.fetch(
    documentUploadUrl,
    options
  );

I’m quite certain I have the correct invoice id, so I assume something about my request is malformed? Can anyone shed more light onto the No query results for model error?

Thanks!

Hi,

Try changing the Content-Type to application/json

Thanks for the response hillel!

With 'Content-Type': 'application/json', I get the message:

{"message":"Method not supported for this route"}

Here’s a sample cURL request for v5

curl -X POST https://domain.com/api/v1/invoices/wMvbmgreYA?include=documents \
  -H 'Content-Type: multipart/form-data' \
  -H 'X-API-TOKEN: TOKEN' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -F _method=PUT \
  -F 'documents[][email protected]'

I think content-type should be changed back

Thanks @hillel. I tried pretty much that exact same cURL and I get the same error message as I received with the JS request:

curl -X POST https://invoicing.co/api/v1/invoices/[invoice_id]/upload?include=documents \
>   -H 'Content-Type: multipart/form-data' \
>   -H 'X-API-TOKEN: TOKEN' \
>   -H 'X-Requested-With: XMLHttpRequest' \
>   -F _method=PUT \
>   -F 'documents[]=@Desktop/test.txt'

Response:

{"message":"No query results for model [App\\Models\\Invoice]."}

I did add back the /upload to the example you provided. Is the query param ?include=documents important?

I have this working now. I’m not exactly sure what the fix was, but possibly creating a new API key after I had upgraded to the Enterprise plan was the catalyst. Here is my now working code with Google Apps Script (JavaScript):

const uploadSummarySheetToInvoice_ = (invoiceId, summarySheetFileId) => {
  const summaryFileBlob = DriveApp.getFileById(summarySheetFileId).getBlob();
  const uploadOptions = {
    method: "post",
    headers: INVOICE_NINJA_REQUEST_HEADERS,
    payload: {
      _method: "PUT",
      "documents[]": summaryFileBlob,
    },
  };
  const documentUploadUrl =
    INVOICE_NINJA_API_PATH +
    "/invoices/" +
    invoiceId +
    "/upload?include=documents";

  const invoiceCreateResponse = UrlFetchApp.fetch(
    documentUploadUrl,
    uploadOptions
  );
  Logger.log("Response body: ");
  Logger.log(invoiceCreateResponse.getContentText());
};

Thanks for the help @hillel

1 Like

Glad to hear it, thanks for the update!