Optic provides you with the ability to build scripts around your API Ops process. Sometimes you may want to access data that Optic gathers as it's documenting your APIs in your scripts. This might include the spec itself or changes to the spec. Optic provides a tool called Spectacle to make this possible.
Spectacle is a GraphQL API that enables you to interact with Optic's data, either through exploring it in your browser with GraphiQL or querying in your code using Optic scripts.
To open GraphiQL for your locally-running instance of Spectacle, run the CLI command:
This will ensure Spectacle is running and open your browser to the Spectacle URL where you can explore the API and its documentation.
The Optic scripts expose an environment variable
SPECTACLE_URL for querying Spectacle. You can use this in your scripts to query the locally-running API.
We'll look at an example using a Node.js script. The one below is using the
requests query to get information about requests and responses in the spec. It's using
node-fetch as the library to query Spectacle.
- Install Node.js if you haven't already
- Save this code above as
npm install node-fetch
Next we need to define an Optic script to run our code.
Now run the script using:
After running this, you should see the data printed in the terminal.