Examples
Adapt these examples: replace the topic names with your own, hard-to-guess ones.
End of a script
Add a line at the end of a long job.
./convert-videos.sh
curl -d "Conversion complete" https://usenotif.com/my-scripts
To tell success from failure:
if ./convert-videos.sh; then
curl -H "Tags: white_check_mark" -d "Conversion succeeded" https://usenotif.com/my-scripts
else
curl -H "Tags: x" -H "Priority: high" -d "Conversion failed" https://usenotif.com/my-scripts
fi
Backup
The message can contain the output of a command.
#!/bin/sh
if restic backup /home; then
curl -H "Title: Backup" -H "Tags: floppy_disk" -d "Backup of $(date +%F) complete" https://usenotif.com/my-backups
else
curl -H "Title: Backup" -H "Tags: x" -H "Priority: urgent" -d "Backup of $(date +%F) failed" https://usenotif.com/my-backups
fi
To attach the log:
curl -T backup.log -H "Filename: backup.log" -H "Message: Last night's log" https://usenotif.com/my-backups
Cron
This job sends a reminder every Monday at 8 a.m.
0 8 * * 1 curl -s -H "Tags: bell" -d "Take out the recycling" https://usenotif.com/my-home > /dev/null
This one notifies you if disk usage goes over 90%.
#!/bin/sh
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE" -gt 90 ]; then
curl -H "Title: Disk space" -H "Priority: high" -H "Tags: warning" -d "Disk $USAGE% full" https://usenotif.com/my-server
fi
Order in an online shop
Most online shops send a JSON webhook for each order. A template in the address is enough, with no code to write. Use this address as the webhook URL:
https://usenotif.com/my-shop?tpl=yes&t=Order+%7B%7B.order_number%7D%7D&m=Total+%24%7B%7B.total%7D%7D&ta=shopping_cart
To try it, simulate the webhook. Adapt order_number and total to the fields your shop sends.
curl -d '{"order_number":1042,"total":"59.00"}' "https://usenotif.com/my-shop?tpl=yes&t=Order+%7B%7B.order_number%7D%7D&m=Total+%24%7B%7B.total%7D%7D&ta=shopping_cart"
You get the title Order 1042 and the message Total $59.00, with a shopping cart icon.
Sensor at home
A Raspberry Pi can watch a sensor and notify you.
#!/bin/sh
TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)
TEMP=$((TEMP / 1000))
if [ "$TEMP" -gt 70 ]; then
curl -H "Title: Temperature" -H "Tags: thermometer" -H "Priority: high" -d "$TEMP degrees" https://usenotif.com/my-home
fi
A small device that can only call an address can use a GET request:
curl "https://usenotif.com/my-home/publish?message=Water%20in%20the%20basement&priority=urgent&tags=droplet"
Scheduled reminder
curl -H "Delay: 45m" -H "Tags: bell" -d "Take the cake out of the oven" https://usenotif.com/my-kitchen
curl -H "At: tomorrow, 7am" -H "Timezone: America/Toronto" -d "Pick up the tickets" https://usenotif.com/my-reminders
Python
The standard library is enough. JSON keeps accented characters intact.
import json
import urllib.request
body = {
"topic": "my-scripts",
"title": "Job complete",
"message": "1,200 files processed",
"priority": 4,
"tags": ["white_check_mark"],
}
request = urllib.request.Request(
"https://usenotif.com",
data=json.dumps(body).encode("utf-8"),
method="POST",
)
urllib.request.urlopen(request)
With a protected topic, add headers={"Authorization": "Bearer tk_yourtoken"} to Request.
JavaScript
This code works in Node.js and in a browser.
await fetch('https://usenotif.com', {
method: 'POST',
body: JSON.stringify({
topic: 'my-scripts',
title: 'Job complete',
message: '1,200 files processed',
priority: 4,
tags: ['white_check_mark'],
}),
});
To receive messages in a web page:
const stream = new EventSource('https://usenotif.com/my-scripts/sse');
stream.onmessage = e => {
const m = JSON.parse(e.data);
if (m.event === 'message') console.log(m.title, m.message);
};
PowerShell
In Windows PowerShell, curl is a different program: use Invoke-RestMethod. Convert the body to UTF-8 to keep accented characters.
$body = @{
topic = 'my-scripts'
title = 'Backup complete'
message = 'Copy successful'
priority = 4
tags = @('floppy_disk')
} | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri 'https://usenotif.com' -Body ([Text.Encoding]::UTF8.GetBytes($body))
For a simple message:
Invoke-RestMethod -Method Post -Uri 'https://usenotif.com/my-scripts' -Headers @{ Title = 'Disk'; Priority = 'high'; Tags = 'warning' } -Body ([Text.Encoding]::UTF8.GetBytes('Disk almost full'))
In the response shown, accented characters may look wrong. The message your subscribers receive is correct.
GitHub Actions
Add this step at the end of a job. if: always() runs it even if the job fails.
- name: Notify
if: always()
run: |
curl -H "Title: ${{ github.repository }}" -H "Tags: package" \
-d "Build ${{ job.status }} on ${{ github.ref_name }}" \
https://usenotif.com/my-builds
For a reserved topic, keep your token in a repository secret:
- name: Notify
if: always()
run: |
curl -H "Authorization: Bearer ${{ secrets.NOTIF_TOKEN }}" \
-d "Build ${{ job.status }}" \
https://usenotif.com/my-builds
To follow a repository's events without touching your workflows, see the GitHub template.