Attaching PDF Document Through The API


I was wondering if it was possible to attach a PDF file to an invoice through the API. I see that there is a documents POST request in the API documentation but I cannot seem to get it working. Every time I try I get an error 500 (Internal Server Error). My goal is for this PDF to be attached to the email sent from invoice ninja. If it is possible, any examples would be greatly appreciated.

Thank you


It’s supported but I don’t have an example to share.

If you’re selfhosting there may be more details in the logs in storage/logs/laravel-error.log

Is there any more API documentation on this other than the one here:

I am not running a self hosted environment.

Thank you.

That’s all we have for the current version.

The next version has more complete docs but it isn’t yet available on the hosted platform.

I do not see a document section in the documentation you provided. Is the pdf attached differently in this version of the API?

Here’s a sample cURL request for v5

curl -X POST \
  -H 'Content-Type: multipart/form-data' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -F _method=PUT \
  -F 'documents[]=@filename.png'

That example helps for v5 but as you said this is not supported in a non-self-hosted environment. Can you provide a curl example for a hosted environment? I tried running the following curl command using the documentation for the hosted environment and got the internal server error:

curl -X POST HOSTEDURL/public/api/v1/documents
-H "X-Ninja-Token: TOKEN" 
-H "X-Requested-With: XMLHttpRequest" 
-H "Content-Type:application/json" 
-d '{"id":1, "name":"FabrikamInc._STDINV2358.pdf", "type":"pdf", "invoice_id":7, "updated_at": 1451160233, "archived_at": 1451160233}'

Sorry, I don’t have an example for v4

So how do you know that this functionality is supported in v4? The curl command I just posted above is based on the only documentation you have for this for v4. I copied exactly what to send in from the documentation and it is failing with an internal server error.

I know it’s supported because I built the v4 mobile app which uses the API to upload documents, here’s the code in case it helps.

The v4 and v5 API’s are not compatible so I wouldn’t expect the v5 cURL example to work with v4.

Can you tell me what needs to be sent into the request exactly for v4? The curl request I am sending that is returning the error is based exactly on the documentation. Something is missing from the request. What fields need to be sent in the request to get the file to attach to the invoice?

The app is expecting the file itself, in your example it looks like you’re sending JSON data?

I am sending JSON data because that is what the documentation says to send in (screenshot below). Can you please tell me what format the API is expecting the file to come in?

It’s expecting multipart/form-data as shown in the v5 cURL sample

If you have a chance please try this format

curl -X POST<your_id> \
  -H 'Content-Type: multipart/form-data' \
  -H 'X-Ninja-Token: TOKEN' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -F _method=PUT \
  -F 'file=@filename.png'

I get the same internal server error with this curl

Removing the _method line makes it work in my test

Note: you need to set the invoice_id, token and filename

curl -X POST<your_id> \
  -H 'Content-Type: multipart/form-data' \
  -H 'X-Ninja-Token: TOKEN' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -F 'file=@filename.png'

I just tried that and I am getting the same error.

I was able to find the error log in my invoice ninja environment. Here is the error I keep getting:

[2020-10-27 11:34:32] production.ERROR: ***ErrorException*** [0] : /var/www/html/bluemoontest/app/Ninja/Repositories/DocumentRepository.php [Line 62] => Undefined index: file {"context":"PHP","user_id":3,"account_id":1,"user_name":"Cameron Williams","method":"POST","user_agent":"vscode-restclient","locale":"en","ip":"","count":1,"is_console":"no","is_api":"yes","db_server":"mysql","url":"api/v1/documents"} []

From the error it looks like the file isn’t set, does the file exist in the same folder or is the full path specified?