Using the Neptyne API

Note: This tutorial requires enabling Advanced Features. Head here for more information.

With Neptyne there are so many ways to interact with Sheets.

The Py function lets you run your custom Python functions.
Using the imperative API you can read/write values from Python into your sheet e.g. A1 = 4.
Neptyne also supports streamlit for building full blown user interfaces within Sheets using just Python.

But did you know Neptyne can even interact with your sheets automatically? The two main approaches for that are Neptyne's cron functions, and REST API.

The cron functions (nt.weekly, nt.daily) are the simplest way; They allow you to run functions automatically at a daily or weekly interval. Check out this tutorial on using the cron functions to create a twitter bot that posts daily with r/askreddit summaries.

In this tutorial we'll cover Neptyne's REST API which lets you to send requests to Neptyne to run functions on your sheet. Combined with the imperative API, this is a great way to externally load data into your sheet.

Let's pretend we have an existing ticketing system. We want to hook the flow of when a user submits a ticket to write that ticket into Sheets. The final sheet will look like this:

The first step is to head to the Neptyne Menu and click Advanced Features. Once you've enabled advanced features, you'll see a switch to enable API access to this Sheet. After you toggle it on an API key will be created that you can copy. This API key is specific to just this Sheet, so you'll need to enabled API access per sheet.

Now let's setup our API handler. Take a look at this example code.

import neptyne as nt
import json
from datetime import datetime


@nt.api_function(route="insert_ticket")
def insert_ticket(request):
    info = json.loads(request.body)
    ticket = [datetime.now(), info["topic"], info["details"]]
    A2:C.insert_row(0, ticket)
    return "success"

The nt.api_function decorator is what allows this specific function to be externally callable. Functions without this decorator cannot be called from the API, so rest assured that only the specific flows you configure will be used for API access.

We then receive a web request, and parse the body. We use the data there to create a ticket row and insert that into our Sheet. Let's use curl from a terminal to send a request.

curl -X POST 'https://app.neptyne.com/api/v1/tynes/{SHEET_ID}/functions/insert_ticket?apiKey={API_KEY}'
-H 'Content-Type: application/json'
--data '{"topic":"Need help","details":"Help I am trying to use the Neptyne API!"}'

You'll need to fill in your SHEET_ID from the sheets url (https://docs.google.com/spreadsheets/d/{SHEET_ID}), and the API_KEY with the one you copied over before from advanced features.

And now we're all set! When we send in API requests, the Python kernel will automatically wake up and process the request. Keep in mind the first request might be a little slow as the kernel is initialized, but subsequent requests will run more quickly.

We're excited to see what you'll create with this new API. Some ideas:
- Slack bot using Neptyne/Sheets as the webhook.
- Chrome extension to insert items you're interested in to track prices.
- Energy monitoring system to see when the lights are on in your home/office.

Advanced Features Tutorials