Using Flask with Optic

Optic is easy to use with Flask, no special library or large code changes required.

api start Command#

Let's say we have a simple Flask server that we usually develop with on localhost:3005.

Optic needs to know how to start our API and control the port it starts on#

Our optic.yml file would include our start command (such as flask run --port=$PORT). The start command in this case also sets the port to which the application binds. Optic injects a $PORT environment variable for our application to listen on when api start is run. Flask accepts the --port option, which sets the port on which the application will bind. No code changes should be required.

name: Flask API
command: flask run --port=$PORT
inboundUrl: https://localhost:3005

Suppose your flask application typically runs on port 3005 (as above). Running api start will fire up:

  • Your main application on a port assigned to it by optic in the $PORT variable
  • The optic proxy listening to requests specified on the url provided by the inboundUrl parameter in the YAML above

Make sure these are different ports - your requests should still be sent to the inboundUrl, but now optic will capture them and look for diffs.

Verifying with api check start#

The setup tool will guide you through running checks to assure Optic is running successfully with your API project. You should see everything pass at this point. If you don't, it's most likely that you're not starting the API on the $PORT Optic provides. The Optic check will also report any other common issues it detects and give you steps and suggestions to resolve them.

Should you need any more help, or want to chat about the process, please reach out to us. You can schedule a quick chat with the maintainers or open an issue on GitHub.

Run your API through Optic#

At this point you should be ready to run api start and document your API with Optic! Optic will observe your API traffic locally, and report on the routes and traffic shape it sees in the Optic dashboard. The next step is to start building your baseline specification

Contributions to this article from: @acunniffe @LouManglass