-

@ cadayton
2025-03-29 17:04:41
#btcpayserver #lightning #lnd #powershell
<p style="text-align: center; font-size:160%;">BTCpayAPI now supports file upload</p>
<p><img style="display: block; margin-left: auto; margin-right: auto; border-width: 10px; border-style: hidden;" src="https://cadayton.onrender.com/images/BTCpayApi800.png" alt="" width="800" height="300" /></p>
I'm continuing to add functionality to BTCpay and BTCpayAPI which is using REST Api(s) to manage my BTCPAY server and LND cloud instance. It is nice to have this just running locally on my home Linux desktop.
Here is the code that implements this functionality.
```
"Uploadfile" {
$apislug = "api/v1/files"
#$filepath = Split-Path $options
$filename = Split-Path $options -Leaf
# CONST
$CODEPAGE = "iso-8859-1" # alternatives are ASCII, UTF-8
# Read file byte-by-byte
$fileBin = [System.IO.File]::ReadAllBytes($options)
# Convert byte-array to string
$enc = [System.Text.Encoding]::GetEncoding($CODEPAGE)
$fileEnc = $enc.GetString($fileBin)
# We need a boundary (something random() will do best)
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,
"--$boundary--$LF"
) -join $LF
$URI = $BTCPayCfg.BTCpayApi.GreenApi.url + $apislug
$apiKeyToken = 'token ' + $script:BTCPAY_API
$headers = @{'Authorization'=$apiKeyToken}
return Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
}
```
Here is the revision history since my last article.
```
Version Date Whom Notes
======= ========== ======== =====================================================
0.1.4 03/28/2025 cadayton Added UploadFile method to upload a file to the BTCpay server
0.1.3 03/27/2025 cadayton Added GetFiles returns listing of files uploaded to BTCpay server
0.1.2 03/19/2025 cadayton ForwardingHistory new parameter "total_fees" tallys mfees for events returned
0.1.1 03/18/2025 cadayton ForwardingHistory now support additional parameters
0.1.0 03/15/2025 cadayton initial release.
```
The inspiration for this logic was adapted from [weipah](https://gist.github.com/weipah/19bfdb14aab253e3f109) . One wouldn't think uploading a single file wouldn't require this much coding. The Greenfield's REST Api documentation for this end point wasn't very helpful.
In my book, good end-user documentation of one's code is just as important as the code itself. I believe documentation is
usually lacking on most projects because the effort of doing so is almost equal to the effort of writing the code. It is also the least fun part about writing code.
The job is not done until the paper work has been completed. :)
https://btcpayserver.sytes.net