Jun 9, 2021

3 min read

Creating an API with Daeploy

In a previous blog post, you learned from our data scientist, Oskar Liew, about REST APIs and how they are a way for programs to communicate with each other using URLs. In this tutorial, we will explain how to create an API using Daeploy.

Creating an API

Creating an API with Daeploy is no harder than writing a regular python function. You simply decorate the functions you want to expose to the API with the entrypoint decorator, like this:

# service.py from daeploy import service @service.entrypoint def hello(name: str) -> str: return f"Hello {name}" if __name__ == "__main__": service.run()

If you run this file, it will start a web-server on http://localhost:8000 and an interactive API documentation will be generated on http://localhost:8000/docs. Daeploy looks at the signature of the function and uses type hints to show expected data types in the documentation and adds type validation for the API.

To test an entrypoint, you can click the “try it out” button in the top right and change the input to something like {“name”: “Bob”} and click execute to send the request to the server. You can scroll down a bit to see the response.

And that is how simple it is to create an API with Daeploy in python. If you have access to a machine with Docker it is also very easy to deploy the API as a containerized service, which we will look at in the next section.

Deploying the API as a Service

This section requires you to have Docker installed on your machine, but the process is similar if you want to deploy to a remote machine. The first step is to start the Daeploy manager, which is available as a free docker image:

$ docker run -v /var/run/docker.sock:/var/run/docker.sock -p 80:80 -p 443:443 -d daeploy/manager:latest

Once the manager container is up and running, you can log in to it with the Daeploy command-line interface (CLI) with the login command.

$ daeploy login -host http://localhost

To create a new project, we use:

$ daeploy init --name hello_service

This creates all the necessary boilerplate for you, so you can focus on the actual code. One of the files is called service.py and this is where the code for the service is written by default. You can replace the contents of that file with the API code we wrote earlier. Once that is done, the service is ready to deploy. We can do this with the deploy command of the CLI. It takes three inputs: service name, service version, and path to the service folder.

$ daeploy deploy hello 1.0.0 ./hello_service/

Deployment takes a few seconds, as Daeploy builds a docker image from the service code and starting it. Once deployment is finished, you can see your service deployed on the manager dashboard on http://localhost. From the dashboard you get a link to the logs and interactive documentation of the service where you can test it in the same way as we did earlier.

That is all that is required in order to build an API with the Daeploy SDK and deploy it as a managed container service on your own machine. The beauty of Daeploy is that it is just as easy to deploy the same service to any machine running a manager. Removing the difficulty of managing development and deployment environments, without requiring any DevOps experience. I hope you have as much fun using Daeploy as we had built it! Happy Daeploying!

Originally published at https://daeploy.com on June 9, 2021.