Go-daraja is an open-source project facilitating seamless integration of Safaricom's Daraja API into Golang applications, providing developers with a straightforward interface. It simplifies API interaction, allowing developers to focus on core application logic while encouraging community collaboration for ongoing improvement.
-
Ensure that go is installed. In a situation where go is not installed click install go
-
Create a new go project
mkdir duka-letu cd duka-letu go mod init github.com/user-github-name/project-name
cp example/.env .env
-
Install go-daraja package
go get "github.com/silaselisha/go-daraja"
A sample implementation of MPESA STK push intergartion in a go project
import (
"log"
daraja "github.com/silaselisha/go-daraja/pkg/handler"
)
func main() {
// create a client by passing the path to your .env file
client, err := daraja.NewDarajaClient(".")
if err != nil {
log.Panic(err)
}
// invoke STK/NI Push
res, err := client.NIPush("test STK push", "0708374149", 1)
if err != nil {
log.Panic(err)
}
log.Printf("%+v\n", res)
}
Note:
Majority of the services provided by daraja API require the client to be authenticated before invoking them. With go-daraja a simplified interface Daraja
shall provide the user with all necessary services.