API Bug Report – 500 Error When Uploading Expense with Receipt Attachment

I’m encountering a consistent issue with the /v2/expenses API endpoint when attempting to upload an expense that includes a receipt attachment. The API call works perfectly without the attachment, but as soon as I include any file — even a basic .txt — the server returns a 500 Internal Server Error.

:white_check_mark: Working Example (No Attachment)

POST https://api.freeagent.com/v2/expenses

{
  "expense": {
    "user": "https://api.freeagent.com/v2/users/1915661",
    "category": "https://api.freeagent.com/v2/categories/285",
    "dated_on": "2025-04-21",
    "description": "Test expense no attachment",
    "gross_value": 100,
    "currency": "GBP"
  }
}

:white_check_mark: Response: 201 Created


:cross_mark: Failing Example (With Attachment)

When adding a simple file like this:

'receipt' => new CURLFile('dummy_receipt.txt', 'text/plain', 'dummy_receipt.txt')

The same request structure returns:

HTTP/1.1 500 Internal Server Error

This occurs regardless of file type — .pdf, .txt, .jpg — and from multiple servers and environments.

:test_tube: Troubleshooting Done

  • Token, user, and category are all valid
  • Same payload used across tests
  • Curl verbose log confirms SSL and payload are sent correctly
  • Attachment encoding verified (using CURLFile)
  • Error occurs only when receipt is attached

Could you help identify whether there’s an issue on the server side or something unexpected required in the upload format?

Happy to provide full curl output and debug logs if useful.

Thanks in advance!

Hi, I’m James one of the FreeAgent engineers.

The parameter to add a file to the expense is attachment and you can specify the content by specifying a few fields including the data in Base64 encoding

For example,

{
  "expense": {
    ...,
    "attachment": {
      "file_name": "dummy_receipt.png",
      "description": "My dummy receipt",
      "content_type": "image/png",
      "data": <Base64 encoding of the file content>
    }
}

Note, we only accept images or PDFs (the valid content types are listed in the API docs).

I’m not familiar with how you generate the Base64 content using PHP but (from your user name; assuming you’re on a mac) you could do

cat dummy_receipt.png | openssl base64 | tr -d `\n`

which will output the contents of the file in the correct encoding (with the newlines that openssl adds filtered out).

Hopefully that will get you a bit further but let us know if you have any further questions.

Cheers,
James