Skip to content

An example application that demonstrates how to use Comlinks and OneSDK in a Python production environment

License

Notifications You must be signed in to change notification settings

superfaceai/python-production-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Website | Get Started | Documentation | GitHub Discussions | Twitter | Support

Superface

Superface Comlink & OneSDK Production Example

This example demonstrates how to use Comlink files created using the Superface CLI in a production setting with OneSDK and Python.

Overview

The use case in this example is "sending an email". To do this, we are using the API of the email provider Resend.com.

The associated Comlink profile and Comlink map required by OneSDK for this use case were generated using the Superface CLI. You can see those files in the superface directory.

This example is a Python application using Flask to create a server that will accept POST requests to an /execute endpoint, which will then invoke OneSDK to perform the use case tasks and send an email.

Prerequisites

To work with this example you will need Python 3.8 or higher, as well as PyPi.

To use Resend as the email API provider, you will need to register an account and create an API key. You must also verify the domain you want to send from, otherwise sending will be limited to the email address of the account holder only.

Running the example

Start by cloning this repository to your local machine:

git clone https://github.com/superfaceai/python-production-example.git

Install the dependencies:

pip install -r requirements.txt

Create a .env file and add your SUPERFACE_ONESDK_TOKEN and the RESEND_TOKEN for the Resend API:

SUPERFACE_ONESDK_TOKEN=
RESEND_TOKEN=

Run the server:

FLASK_APP=server flask run

This will run the development server using Nodemon on port 5000. The /execute endpoint is now available.

Test the endpoint

With the server running, make a POST request using a tool or your choice such as Postman, HTTPie or RapidAPI, to:

http:https://localhost:5000/execute

With the following JSON object as the body:

HINT: Don't forget to replace the email addresses with real ones.

{
  "to": "replace-with-your-email",
  "from": "replace-with-your-from-email",
  "subject": "Hello, World!",
  "text": "Hello, from Superface!"
}

For example, using cURL:

curl -X POST http:https://localhost:3000/execute -H 'Content-Type: application/json' -d '{ "to": "replace-with-your-email", "from": "replace-with-your-from-email", "subject": "Hello, World!", "text": "Hello, from Superface!"}'

On success you will see the id of the email sent returned from Resend.

If the request fails, you will get an error response from OneSDK.

Deploying to production

If everything works locally, the next step would be to modify any aspects of server.py to your liking and deploy it to production.

Example - Deploy to Fly.io

Fly.io is a service that can handle fast deploys hosted in regions of your choice. For example purposes, here's how to deploy this application with them.

Install the Fly.io tools

brew install flyctl

Launch a new machine:

fly launch

Deploy the app to Fly.io

fly deploy

Then, add the content from your .env file to the ENV on the server:

fly secrets set SUPERFACE_ONESDK_TOKEN=yourtoken

fly secrets set RESEND_TOKEN=yourresendtoken

After a successful deployment you will be able to access the same /execute endpoint at:

https://<your-app-name>.fly.dev/execute

License

OneSDK and this example application are licensed under the MIT License.

© 2023 Superface s.r.o.

About

An example application that demonstrates how to use Comlinks and OneSDK in a Python production environment

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published