AstroNote

QuickstartExamplesAPIFAQ     Devices & Tokens    

Code Examples

Introduction

The AstroNote API is language and platform agnostic. We will show two examples here, one usage from the command line with the curl command and one through Python.

These examples will easily translate to any other language that can make HTTP requests.

Check the full API Documentation for an overview of all available API parameters.

Examples

Python Example

This example uses the popular requests library.

import requests

headers = { "Authorization": "token YOURTOKEN" }

notification = {
  "title": "Your coffee is ready",
  "body": "Double strong, double sweet."
}

r = requests.post("https://api.astronote.app/1/notify",
  headers=headers, json=notification)
r.raise_for_status()

Substitute YOURTOKEN with the API Token that you copied from the Devices & Tokens page.

Curl Example

This Curl example uses a Form POST, which may be simpler to use from a shell script.

curl "https://api.astronote.app/1/notify" \
    -H "Authorization: token YOURTOKEN" \
    -d "title=Transporter Failure" \
    -d "body=The pattern buffer is full." \
    -d "category=Transporter Failures"

Substitute YOURTOKEN with the API Token that you copied from the Devices & Tokens page.

Node Example (with node-fetch)

This Node example uses the node-fetch module.

const fetch = require("node-fetch");

const sendNotification = async (token, title, body) => {
  const response = await fetch("https://api.astronote.app/1/notify", {
    method: "POST",
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `token ${token}`
    },
    body: JSON.stringify({ title: title, body: body, persistent: false })
  });
  return response.status;
};

sendNotification("YOURTOKEN", "Note from Quark's",
    "Your freshly brewed Raktajino is ready for pickup.");```

Substitute YOURTOKEN with the API Token that you copied from the Devices & Tokens page.

Go Example

package main

import (
    "log"
    "net/http"
    "net/url"
)

func main() {
    values := url.Values{
        "title": {"Red Alert!"},
        "body": {"A Klingon bird of prey just decloaked!"},
        "token": {"YOURTOKEN"},
    }

    endpoint := "https://api.astronote.app/1/notify"
    if _, err := http.PostForm(endpoint, values); err != nil {
        log.Fatal("Failed to post: ", err)
    }
}

Substitute YOURTOKEN with the API Token that you copied from the Devices & Tokens page.

Perl 5 Example

use HTTP::Tiny;
use JSON::PP;

my %HEADERS = ("Content-Type" => "application/json", "Authorization" => "token YOURTOKEN");
my $uri = "https://api.astronote.app/1/notify";
my $request = HTTP::Tiny->new('default_headers' => \%HEADERS);
my $body = JSON::PP->new->encode({
    title => 'Transporter Failure',
    body => 'The pattern buffer is full',
    category => 'Transporter Failures'
});
my $response = $request->post($uri, {'content' => $body});
print "$response->{'status'} $response->{'reason'}\n”;

Substitute YOURTOKEN with the API Token that you copied from the Devices & Tokens page.