Created
August 14, 2025 17:00
-
-
Save driftywinds/c238bf96e46ecdc193fa36a8c4b260e1 to your computer and use it in GitHub Desktop.
Open powershell, type "notepad $profile" and paste the contents of this gist in there (change the env variables at the top)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #creator = Hefty-Possibility625 on Reddit, more here - https://www.reddit.com/r/selfhosted/comments/1mohkot/i_love_self_hosting_memos_great_api_for_super/ | |
| $env:MEMOS_URI = 'https://YOUR_DOMAIN.com' | |
| $env:MEMOS_TOKEN = 'YOUR_API_TOKEN' | |
| function memo { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] | |
| [string]$content, | |
| [Alias('tags')] | |
| [string[]]$tag, | |
| [string]$MemoUri = $env:MEMOS_URI, # e.g. https://memos.example.com | |
| [string]$ApiToken = $env:MEMOS_TOKEN | |
| ) | |
| if ([string]::IsNullOrWhiteSpace($MemoUri)) { | |
| throw "MemoUri is required. Pass -MemoUri or set MEMOS_URI." | |
| } | |
| if ($MemoUri -notmatch '^https?://') { | |
| $MemoUri = "https://$MemoUri" | |
| } | |
| if ([string]::IsNullOrWhiteSpace($ApiToken)) { | |
| throw "ApiToken is required. Pass -ApiToken or set MEMOS_TOKEN." | |
| } | |
| # Normalize tags: split on commas and/or whitespace, remove empties, ensure one leading # | |
| $tagList = @() | |
| if ($tag) { | |
| foreach ($t in $tag) { | |
| $tagList += ($t -split '[\s,]+') | |
| } | |
| $tagList = $tagList | | |
| Where-Object { $_ -and $_.Trim() -ne "" } | | |
| ForEach-Object { | |
| if ($_ -match '^\#') { $_ } else { "#$_" } | |
| } | |
| # If you want to dedupe, uncomment the next line: | |
| # $tagList = $tagList | Select-Object -Unique | |
| } | |
| $tagString = if ($tagList) { $tagList -join " " } else { "" } | |
| $body = @{ | |
| content = if ($tagString) { "$content`n$tagString" } else { $content } | |
| } | ConvertTo-Json -Depth 10 -Compress | |
| $headers = @{ | |
| Accept = 'application/json' | |
| Authorization = "Bearer $ApiToken" | |
| } | |
| try { | |
| $response = Invoke-RestMethod -Method Post ` | |
| -Uri ($MemoUri.TrimEnd('/') + "/api/v1/memos") ` | |
| -Headers $headers ` | |
| -ContentType 'application/json' ` | |
| -Body $body | |
| } catch { | |
| Write-Error ("memo: HTTP request failed: " + $_.Exception.Message) | |
| return | |
| } | |
| if ($verbose) { $response } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment