How to create a minimal API with Hapi.js, including public and protected routes
This repository demonstrates how to create a minimal API with Hapi.js, including public and protected routes. The protected route is secured using Unkey.
1git clone https://github.com/Yash-1511/hapi-unkey-template.git2cd hapi-unkey-template3npm install
.env
file and add your Unkey root key:1UNKEY_ROOT_KEY=your-unkey-root-key-here
1node server.js
The server will run on http://localhost:3000
.
GET /public
- This is a public route accessible by anyone.GET /protected
- This is a protected route, requiring a valid Unkey token.You can access the public route by making a simple GET
request:
1curl http://localhost:3000/public
You should get a response:
1{2 "message": "This is a public route."3}
To access the protected route, you need to include the Unkey token as a bearer token in the request header. Replace your-unkey-api-key
with the actual key you generated in Unkey.
1curl -H "Authorization: Bearer your-unkey-api-key" http://localhost:3000/protected
If the token is valid, you will get a response like this:
1{2 "message": "This is a protected route, secured by Unkey."3}
If the token is missing or invalid, you will get an error response:
1{2 "error": "Invalid token"3}
Authorization: Bearer <token>
header.This project is licensed under the MIT License.
2500 verifications and 100K successful rate‑limited requests per month. No CC required.