Tutorial: Building a GET API with WayScript

tjmd

TJ-MD

Posted on May 5, 2020

Tutorial: Building a GET API with WayScript

Introduction

WayScript makes it really easy to make a fully fleshed out APIs in minutes. Today we'll be be discussing how to make a GET API.

Prerequisites

In order to make the API, we'll utilize an HTTP Trigger, a SQL Module, a Python Module, and an HTTP Response. There are no prerequisites but some content you might find helpful:
Working with HTTP
Working with Python
Working with SQL

Building the GET API

With WayScript, you can set up a workflow really quickly.
New Script

You can create a workflow by clicking the + New Script Button in the top right corner of the website.

In this Script, to make a workflow he using the HTTP Trigger module, which will make a request to our database any time we hit a valid endpoint. Modules are sort of like building blocks, to help form the foundation of our application. After adding the HTTP Trigger Module to the workflow, click on the module and go to the window in the left to add endpoints and query paramters for your API.

add endpoint

After adding the query parameters we can get going with connecting to our SQL database and using the query parameters from our HTTP Trigger.

First, click the SQL Module. WayScript allows you to sink up with a MySQL, PostgresSQL, or SQL Server DB and is adding new ones all the time.

MySQL

After you put in your DB info you. You can query it and alter the qurey so that you can use one of the parameters from your HTTP Trigger.

Query parameter

Now we can query the database to get the entry that we want! Make sure to set up indexes at the columns you would like to use.

Next you need to take the information from your Database response and turn it into a JSON response with the Python Module.

Python code

Click on the Python Module and create a JSON response and put it in your dictionary as resp so you can reference it later.

WayScript type checks your dictionary variables so they make it really easy to know if you formatted your response correctly.

http response

After you add your response to your HTTP Response module and turn on the HTTP Trigger you're good to go!

Result

If you call or go to the endpoint at the supplied query parameters you can see your response!

valid json

If you would like to see this example app check out this script with GET and POST requests, it is public on the WayScript market place. Also feel free to check out other scripts on the WayScript Market Place.

Here's a great video of Erik Toor building this Script.

💖 💪 🙅 🚩
tjmd
TJ-MD

Posted on May 5, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related