DEV Community

Derrick Sherrill for WayScript

Posted on

Create APIs in Minutes

Introduction

Creating a fully functional API is traditionally a long task, but an industry standard and an absolute must for your technical customers. With WayScript, creating an API has never been easier. Let's take a look at how we can create a simple, fully functional API that you send requests to and receive back your desired information.

Prerequisites

No requirements, but some documentation you may find useful:
Working with Python
Working with HTTP

Creating a API URL

To begin, we first need to consider where we want requests to go. In WayScript, we also need to activate our scripts to output the desired results. We can acomplish both of these tasks with one step. We can create a trigger, which activates our script. Specifically, the http trigger can provide us a URL for requests and also we activate our tasks. We can pull in an HTTP trigger as the head of our workflow:

Create API Tutorial Image

Like previously stated, this will create a url which we can customize, to receive requests at.

Create API image 2

In the above photo, we see that we can also configure our settings to allow parameters to be passed to us. This is crucial for any API.

Handling the request

Once the user has gone to the url, sent a get request, or post request, we need to return some type of response back to the user. This is usually JSON or a success message. We can use several different modules to determine what to send to the user. In this tutorial, let's create a python step which will generate a random number.

Create API image 2

We'll generate this random number using some python code that might look like this:

import json
import random
value_from_url = variables['value']
random.seed(value_from_url)
number = random.randint(0,100000000000)
dict_x = {"number":number}
return_json = json.dumps(dict_x)
variables['return_json'] = return_json

We reference the parameter used in the url by accessing it in the variables dictionary. Then we create our new random number, and store it using the same dictionary.

Sending the Response

Now all we need to do is send the JSON response when the user goes to the url or sends the request. We can do this using the HTTP response module.

Create API image 2

The JSON we created in the python step can now be passed using this module back to the user:

Create API image 2

Questions, Feedback?

As always, if you have any questions please reach out to us on our discord and we would be happy to help. If you want to view this completed script, you can find it here or view a youtube video tutorial on our channel.

Top comments (0)