[Go] Send a SigV4-signed HTTP request to API Gateway with aws-sdk-go-v2

Created on November 28, 2026

This example sends a SigV4-signed HTTP request to an API Gateway endpoint configured for IAM authentication.

package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "net/http"
    "time"

    v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
    "github.com/aws/aws-sdk-go-v2/config"
)

func main() {
    ctx := context.Background()

    cfg, err := config.LoadDefaultConfig(
        ctx,
        config.WithSharedConfigProfile("default"),
    )
    if err != nil {
        panic(err.Error())
    }

    credentials, err := cfg.Credentials.Retrieve(ctx)
    if err != nil {
        panic(err.Error())
    }

    req, err := http.NewRequest(
        http.MethodGet,
        "https://example.execute-api.ap-northeast-1.amazonaws.com/dummy-stage/dummy-endpoint",
        nil,
    )
    if err != nil {
        panic(err.Error())
    }

    hash := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    signer := v4.NewSigner()
    signer.SignHTTP(ctx, credentials, req, hash, "execute-api", "ap-northeast-1", time.Now())

    if err != nil {
        panic(err.Error())
    }

    httpClient := new(http.Client)

    response, err := httpClient.Do(req)
    if err != nil {
        panic(err.Error())
    }

    defer response.Body.Close()

    responseBody, err := ioutil.ReadAll(response.Body)
    if err != nil {
        panic(err.Error())
    }

    fmt.Print(string(responseBody))
}

Explanation

  • config.LoadDefaultConfig creates an aws.Config using the default profile.
  • http.NewRequest creates a *http.Request for a GET request with an empty body to the API Gateway URL.
  • The request body is empty, so hash holds the SHA-256 hash of the empty string for use in the SigV4 signature.
  • v4.NewSigner() creates a *v4.Signer.
  • SignHTTP() signs the request with SigV4. Pass these arguments in order.
    1. The context.
    2. The aws.Credentials retrieved from the loaded aws.Config.
    3. The HTTP request.
    4. The SHA-256 hash of the request body.
    5. The service name, execute-api for API Gateway in this example.
    6. The AWS Region.
    7. The current time.
  • The program sends the signed HTTP request.
Go AWS