Build SQL microservices

WayScript allows you to configure your Lair to build an serverless API endpoint in minutes.


Use the boilerplate code below to create an file in your Lair’s root directory. See File system for more details on how to manipulate files in your workspace file system.
Create a file


import os
import mysql.connector
from wayscript import context
from wayscript.triggers import http_trigger
# Setup:
# These values will need to be replaced with values from your own database
# you can usually find these values in your database settings
# Please reference the Docs of your DB service for more specifics around your DB credentials
mydb = mysql.connector.connect(
# Perform your SQL action here.
# depending on the operation your code may change
sql = "SELECT * FROM earnings;"
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
# These results can be passed via the HTTP response if desired.
# In this template we are just returning the stand HTTP response via the WayScript Docs
# Get payload for request event
request_payload = context.get_event()
# Parse header and request body from request payload
request_header = request_payload['data'].get('headers')
request_body = request_payload['data'].get('data')
# Specify response payload
response_payload = {"hello": "world"}
response_headers = {"content-type": "application/json"}
status_code = 200
# Send response
http_trigger.send_response(data=response_payload, headers=response_headers, status_code=status_code)

Create a requirements.txt file

Create a requirements.txt file

Install in your Terminal

Be sure to install your requirements.txt!
pip install -r requirements.txt


A secret is called from the above python code. This secret needs to be named db_password (or altered in the code above to match whatever you name your secret.) This should be your database's password.
Secrets are automatically added as environment variables

Configure http trigger

Back in the Development Panel, Open your Lair’s Triggers Panel and add a new http trigger. Create a name for your trigger and input the following run command. See Triggers for more details.
Example HTTP Trigger Setup
Trigger Name - You can create any name you want for organization.
Command To Run - The Unix command to run your code.
Path - If you want multiple HTTP Triggers in the same lair, you can create custom URL paths. For example you can have <Endpoint_URL>/call-api and then another HTTP Trigger with the URL <Endpoint_URL>/do-something-else

Test your API in development environment

Navigate to the * endpoint generated by your http trigger to see your simple API in action!

Deploy to production environment

Once you have finished testing, press “Deploy” to create a production environment for your API. Select <Lair_name>.prod in the Lair selector menu and view the http trigger to access your API’s production endpoint. See Hosted environments for more details.
By default, your Lair's endpoints are protected against unauthenticated requests. See Endpoints on how to public expose your endpoints or authenticate using your application key.