Download Invoice
API to download invoice file (PDF or XML) by tracking code, returns base64 encoded file content.
API Endpoint
GET
https://einvoice-api-sandbox.sepay.vn/v1/invoices/{tracking_code}/downloadAPI Request
tracking_codestringrequired
Invoice tracking code
typeenumrequired
File type to download (pdf or xml)
API Response
Success Response (200)
{
"success": true,
"data": {
"file_type": "pdf",
"file_name": "HD_0000589_20251215.pdf",
"content": "JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlL1hPYmplY3Q..."
}
}successboolean
dataobject
Error Handling
400Bad RequestInvalid type parameter (not pdf or xml).
401UnauthorizedMissing or invalid Bearer token.
404Not FoundInvoice not found for tracking_code.
Notes
Note
- The API returns file content as base64 encoded string. You need to decode base64 to save as PDF or XML file.
- The
typeparameter only accepts two values:pdforxml. - Make sure the invoice has been successfully issued before downloading.
Converting Base64 to File
After a successful API call, you need to decode the base64 content and save it as a file. Here is an example with PHP:
PHPDecode Base64 and save file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php// Assuming $response is the API result$response = json_decode($apiResult, true);if ($response['success']) {// Get base64 content from response$base64Content = $response['data']['content'];$fileName = $response['data']['file_name'];// Decode base64 to binary$binaryContent = base64_decode($base64Content);// Check if decode was successfulif ($binaryContent === false) {throw new Exception('Base64 decode error');}// Save file$bytesWritten = file_put_contents($fileName, $binaryContent);if ($bytesWritten === false) {throw new Exception('File write error');}echo "File saved: {$fileName} ({$bytesWritten} bytes)";}
Processing steps:
- Parse JSON response - Convert response to PHP array
- Get base64 content - Access
$response['data']['content'] - Decode base64 - Use
base64_decode()to convert to binary - Save file - Use
file_put_contents()to write to file
Important notes
- Always check the result of
base64_decode()as it may returnfalseif the base64 string is invalid. - Ensure the destination directory has write permission.
- For PDF files, you can verify the
%PDFheader after decoding to confirm the file is valid.
Code Examples
>
>
>
curl --request GET \--url 'https://einvoice-api-sandbox.sepay.vn/v1/invoices/084e179d-d95a-11f0-aef4-52c7e9b4f41b/download?type=pdf' \--header 'Authorization: Bearer REPLACE_BEARER_TOKEN'