[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.LoadDefaultConfigcreates anaws.Configusing thedefaultprofile.http.NewRequestcreates a*http.Requestfor a GET request with an empty body to the API Gateway URL.- The request body is empty, so
hashholds 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.- The context.
- The
aws.Credentialsretrieved from the loadedaws.Config. - The HTTP request.
- The SHA-256 hash of the request body.
- The service name,
execute-apifor API Gateway in this example. - The AWS Region.
- The current time.
- The program sends the signed HTTP request.