Language Examples
Copy-paste examples in Python, Node.js, Go, PHP, and Ruby.
Every nudge is a single POST request. Below are copy-pasteable examples for popular languages. Each assumes you have stored your API token in a NUDGE_TOKEN environment variable. See Getting Started for setup.
All examples hit the same endpoint
POST https://nudge.sh/{token} with a status parameter. See the API Reference for the full parameter list.
Python
Using the requests library:
python
import os
import requests
requests.post(
f"https://nudge.sh/{os.environ['NUDGE_TOKEN']}",
data={"status": "done"},
)A reusable wrapper function:
python
import os, time, functools, requests
def nudge_on_finish(func):
"""Decorator that sends a nudge when the wrapped function completes."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
try:
result = func(*args, **kwargs)
status = "done"
except Exception:
status = "failed"
raise
finally:
requests.post(
f"https://nudge.sh/{os.environ['NUDGE_TOKEN']}",
data={"status": status, "duration": int(time.time() - start)},
)
return result
return wrapperNode.js
Using the built-in fetch API (Node 18+):
javascript
await fetch(`https://nudge.sh/${process.env.NUDGE_TOKEN}`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ status: "done" }),
});An async wrapper for longer tasks:
javascript
async function nudge(status, extra = {}) {
await fetch(`https://nudge.sh/${process.env.NUDGE_TOKEN}`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ status, ...extra }),
});
}
// Usage
const start = Date.now();
try {
await runBuild();
await nudge("done", { duration: Math.round((Date.now() - start) / 1000) });
} catch (e) {
await nudge("failed");
}Go
Using the standard net/http package:
go
package main
import (
"net/http"
"net/url"
"os"
"strings"
)
func sendNudge(status string) error {
data := url.Values{"status": {status}}
req, err := http.NewRequest(
"POST",
"https://nudge.sh/"+os.Getenv("NUDGE_TOKEN"),
strings.NewReader(data.Encode()),
)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = http.DefaultClient.Do(req)
return err
}PHP
Using file_get_contents with a stream context (no dependencies required):
php
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query(['status' => 'done']),
],
]);
file_get_contents('https://nudge.sh/' . getenv('NUDGE_TOKEN'), false, $context);Ruby
Using the standard net/http library:
ruby
require "net/http"
require "uri"
uri = URI("https://nudge.sh/#{ENV['NUDGE_TOKEN']}")
req = Net::HTTP::Post.new(uri)
req.set_form_data("status" => "done")
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }