Chris Larham
Chris Larham

Reputation: 11

How do I include my Swagger Hub API definition in a project deployed on Heroku?

I have written the logic for a backend e-commerce REST API, and I have documented it here: https://app.swaggerhub.com/apis-docs/chris-larham-1983/e-Commerce_Registration_Customers_Addresses_Orders/0.1#/. I would like to include this specification in my project that is deployed on Heroku so that the project and documentation are in one place.

I have configured the Heroku database, populated the 'customers', 'orders', 'products', and 'addresses' table with sample data, and the specified endpoints are working as specified in the document - for example, https://codecademy-e-commerce-rest-api.herokuapp.com/api/products.

So far I have tried downloading the documentation from within my Swagger Hub account in 'html', 'html2', and 'dynamic html' formats, as I thought I would just include a .html document in my project. However, these downloaded documents do not closely resemble the documentation as linked above; the formatting is very different and does not look as professional.

Does anybody know I could include my Swagger Hub API definition in my project so that the documentation and project are both hosted on Heroku?

Thanks in advance for any help/pointers to tutorials.

Upvotes: 0

Views: 497

Answers (1)

Chris Larham
Chris Larham

Reputation: 11

After a couple of hours of searching and configuring, I have solved this problem thanks to an npm package called swagger-ui-express. This package can be found here: https://www.npmjs.com/package/swagger-ui-express.

I downloaded my API specification in the format JSON resolved and added it to my project as a file named swagger.json. Then I followed the configuration information in the swagger-ui-express documentation, uploaded to heroku, and it worked!

The lines of code I added in index.js were:

const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

I hope this helps somebody in the future :)

Upvotes: 1

Related Questions