We want an API that can be tested on the Mac with Swift Playgrounds. The first step is to get a working demo API on it.
I love Swift, so why don’t we use Vapor to run our test API with a server-side Swift app?
If you don’t know how to install it, follow the instructions here. Then, open a terminal on your Mac and type the following:
vapor new demo-api --template=auth-templateYou can try to run the app by using vapor run but at the moment, the macOS 15 beta and Xcode 11 beta are not very reliable to run it.
To be sure that the Vapor app will run with the right Swift version, we’ll use Docker to build and run it. If you don’t have it yet, install Docker on your Mac from here and then run a Docker container with the Vapor code:
cd demo-apicp web.Dockerfile Dockerfiledocker build . -t demo-api:latest
Using the next command, you’ll create your REST API which is listening on the 8080 port:
docker run -p 8080:80 --env ENVIRONMENT=DEV demo-api:latestIf everything goes well you should see the following message: Server starting on http://0.0.0.0:80
Leave the terminal with Docker open and check the API works. Open a new terminal window and type:
The output should be:
{"id":1,"email":"user@example.com","name":"user"}Great, you have done it! You’ve added your first user in the demo API.