package main
import (
"context"
"fmt"
"log"
grantex "github.com/mishrasanjeev/grantex-go"
)
func main() {
ctx := context.Background()
client := grantex.NewClient("your-api-key")
// 1. Register an agent
agent, err := client.Agents.Register(ctx, grantex.RegisterAgentParams{
Name: "Email Assistant",
Description: "Reads and sends emails on behalf of users",
Scopes: []string{"read:email", "send:email"},
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Agent registered: %s (DID: %s)\n", agent.ID, agent.DID)
// 2. Create authorization request
authReq, err := client.Authorize(ctx, grantex.AuthorizeParams{
AgentID: agent.ID,
PrincipalID: "user-123",
Scopes: []string{"read:email", "send:email"},
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Consent URL: %s\n", authReq.ConsentURL)
// 3. Exchange authorization code for token (after user consents)
tokenResp, err := client.Tokens.Exchange(ctx, grantex.ExchangeTokenParams{
Code: "authorization-code-from-callback",
AgentID: agent.ID,
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Grant token: %s\n", tokenResp.GrantToken)
// 4. Verify the token
verified, err := client.Tokens.Verify(ctx, tokenResp.GrantToken)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Token valid: %v, scopes: %v\n", verified.Valid, verified.Scopes)
}