Implementing JWT based Authentication for REST API in Golang with Fiber and jwt-go


Authentication is one of the common things to be considered in building an API. Here is a simple code implementing JWT application in a webserver built with the help of fiber framework.


Go 1.18 version is used for this demo.

Clone the current repository with

git clone

Fiber CLI

A dev server can be started for Fiber using this CLI with live reload.

go install

Package installation

Install the packages used with the commands below

  go get
  go get

Testing application

The user name is fixed as for this demo but in an actual application, the username will be fetched from database and the username may be replaced or combined with other parameters like roles

Using cURL

cURL is a tool installed in linux distributions by default used to fetch data from an endpoint.

Get JWT token

we can get the JWT token by POSTing to /api/auth endpoint

curl \
--data '{"name": "meher", "password": "passwd@123"}' \
-H 'Content-Type: application/json' \

save the output value in environmental variables from the response JSON in the above request which will be something like below


To request public endpoint, we can GETting /api/posts endpoint.

curl http://localhost:3000/api/posts/

To get the protected routes, we can send the token through Authorization Header as Bearer token

curl \
-H "Authorization: Bearer $TOKEN" \


View Github