Connecting APIs to spark cloud without hosting your own middleware web app

Sometimes you might have the need to connect a third party API to your spark core, but you might not have your own web host to publish your own web applications to. There are lots of free hosting services that you can use such as Heroku, but this tutorial is about how to hookup an api to the spark cloud using Zapier.com webhooks. In this example, we’ll be sending bitcoin transactions from coinbase to your sparkcore.

First, here is the code for the core:

void setup() {
    Spark.function("bitcoin", bitcoin);
    Serial.begin(9600);
    pinMode(D7, OUTPUT);
}

void loop() {

}

int bitcoin(String args) {
    Serial.println(args);
    
    digitalWrite(D7, HIGH);
    delay(1000);
    digitalWrite(D7, LOW);
    
    return 1;
}

Next, go to zapier.com and signup for a free account. Then from your dashboard, click the orange ‘Make a New Zap’ button. You’ll be brought to a page to select your action type of trigger and action. You should select ‘webhook’ for both the trigger and action. Under the trigger, you should have ‘Catch Hook’ selected and under the action, you should have ‘POST’ selected. Then click continue.

The next screen will give you your zapier url endpoint that you will give to coinbase. Coinbase will use this url to send bitcoin transaction data to.

Now go to the coinbase website and copy the url from zapier and paste it into your coinbase > merchant settings >Instant Payment Notifications > Callback URL field.

Then click the “Test Now” button to send a sample transaction to Zapier.

Back on Zapier now just click ‘Continue’ on step 3.

You can click ‘Continue’ on step 4 as well. Nothing to be done there. On step 5, you’ll enter all the information about where to send the transaction. In this case, we want to send it to the spark cloud. Fill out the url with your device endpoint. (Make sure to replace {DEVICE_ID} with your sparkcore’s id. The Payload type should be ‘form’. The first Data field we will be sending is the ‘args’ field. Here we can pass data straight into your function on the sparkcore.

When you click on ‘insert fields’ on the args line, you might get a pop up that says zapier couldn’t get sample from your webhook. Make sure you go to coinbase and test the url. Then come back and click ‘OK, I did this’ until zapier says it found your changes.

When you go back to step 5, you’ll see a pulldown with all the values in the json object that coinbase sent to zapier during the test.

You can pick whichever fields you want to send to your core. You can also put a comma inbetween each one to separate them. In this example, I chose to pass 'Order Status (comma) Order Total Btc Cents. Then click the plus sign to the right to add another Data field. Put your spark cloud access token in here.

Leave everything else as the default until you get to step 6 - Test this zap. At this point you should be able to run the test and see the LED on your spark core flash for a second. Zapier will also say “Success” on the button. If you have it hooked up to your USB port, you should also see the transaction information in your debug console.

On step 7, name this zap and click ‘Turn Zap on’ to get things rolling.

At this point, you can go back to coinbase and click the ‘Test Now’ button to have coinbase send another test transaction to zapier, which should then send it off the spark cloud and then off to your core.

Here’s what the USB serial output will look like:

You can connect other APIs besides coinbase using this same method if they have a way to POST data to zapier.

Post below if you have any questions!

8 Likes

Outstanding tutorial @Hypnopompia!!!

Anyone with limited programming experience should take note! Hardware actions triggered by third-party services with zero web coding! :+1: :100:

1 Like

@Hypnopompia

Thank you so much for creating/posting this tutorial. Very impressive turn-around as well! When I have time in the next couple days, I plan to fiddle with this and see what else I can make it do. :slight_smile:

Love the fantastic support in the Spark community!

Darryl

2 Likes

Way cool! Nice write up too.