Website | Get Started | Documentation | GitHub Discussions | Twitter | Support
This example demonstrates how to use Comlink files created using the Superface CLI in a production setting with OneSDK and Python.
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.
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.
Start by cloning this repository to your local machine:
git clone https://github.com/superfaceai/python-production-example.gitInstall the dependencies:
pip install -r requirements.txtCreate 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 runThis will run the development server using Nodemon on port 5000. The /execute endpoint is now available.
With the server running, make a POST request using a tool or your choice such as Postman, HTTPie or RapidAPI, to:
http://localhost:5000/executeWith 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://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.
If everything works locally, the next step would be to modify any aspects of server.py to your liking and deploy it to production.
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 flyctlLaunch a new machine:
fly launchDeploy the app to Fly.io
fly deployThen, 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=yourresendtokenAfter a successful deployment you will be able to access the same /execute endpoint at:
https://<your-app-name>.fly.dev/executeOneSDK and this example application are licensed under the MIT License.
© 2023 Superface s.r.o.
