Generate API Keys in Go
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Go
What: Generate cryptographically secure API keys in Go using <code>crypto/rand</code> for microservices and backend systems. Idiomatic Go implementation with proper error handling.
Try it: Use our interactive Api-keys generator or integrate this code into your Go application.
Generate cryptographically secure API keys in Go using crypto/rand for microservices and backend systems. Idiomatic Go implementation with proper error handling.
Looking for other languages? Check our code examples in
PHP
,
JavaScript
,
Python
,
Java
,
C#
,
C++
and
Ruby
or use our interactive web generator.
Go Code Example
package main
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
)
// GenerateAPIKey generates a hexadecimal API key
func GenerateAPIKey(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", fmt.Errorf("failed to generate API key: %w", err)
}
return hex.EncodeToString(bytes), nil
}
// GenerateAPIKeyBase64 generates a base64 URL-safe API key
func GenerateAPIKeyBase64(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", fmt.Errorf("failed to generate API key: %w", err)
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(bytes), nil
}
func main() {
// Generate 256-bit (32 byte) API key
apiKey, err := GenerateAPIKey(32)
if err != nil {
panic(err)
}
fmt.Println("Hex:", apiKey)
// Output: "a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9"
// Base64 URL-safe encoding
apiKeyBase64, err := GenerateAPIKeyBase64(32)
if err != nil {
panic(err)
}
fmt.Println("Base64:", apiKeyBase64)
// Output: "p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo"
}
[EXPLANATION]
Go's crypto/rand package provides cryptographically secure random byte generation using the operating system's CSPRNG. The implementation uses proper error handling with wrapped errors. base64.URLEncoding.WithPadding(base64.NoPadding) creates URL-safe encoding without padding characters, which is ideal for API keys used in URLs.
Expected Output
Hexadecimal: a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9 Base64 URL-safe: p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo With prefix: sk-live-p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo
Common Use Cases
- Go microservices authentication
- gRPC service-to-service auth
- Gin/Echo REST API authentication
- Cloud-native application keys
- Kubernetes operator authentication
Important Notes
-
Always handle errors from
crypto/rand.Read() -
hex.EncodeToString()for hexadecimal encoding -
Use
base64.URLEncodingfor URL-safe keys -
WithPadding(base64.NoPadding)removes = padding - Use bcrypt or Argon2 to hash keys before database storage
Try Our Interactive Generator
Don't want to write code? Use our free web-based Api-keys generator with instant results.
TRY API-KEYS GENERATOR →Other Programming Languages
View Api-keys generation code examples in PHP
View Api-keys generation code examples in JavaScript
View Api-keys generation code examples in Python
View Api-keys generation code examples in Java
View Api-keys generation code examples in C#
View Api-keys generation code examples in C++
View Api-keys generation code examples in Ruby