Quick Start
Generate a QR code in Go using the standard library:
Go - Basic Example
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
type QRRequest struct {
Data string `json:"data"`
Size int `json:"size"`
Format string `json:"format"`
}
func GenerateQRCode(data string, size int) ([]byte, error) {
apiKey := os.Getenv("QR_API_KEY")
reqBody := QRRequest{
Data: data,
Size: size,
Format: "png",
}
jsonBody, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST",
"https://www.qrcodeapi.io/api/generate",
bytes.NewBuffer(jsonBody))
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func main() {
qrCode, err := GenerateQRCode("https://example.com", 300)
if err != nil {
panic(err)
}
os.WriteFile("qrcode.png", qrCode, 0644)
}
HTTP Handler Example
Serve QR codes dynamically in your Go web server:
Go - HTTP Handler
func QRCodeHandler(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
if url == "" {
http.Error(w, "url parameter required", http.StatusBadRequest)
return
}
qrCode, err := GenerateQRCode(url, 300)
if err != nil {
http.Error(w, "Failed to generate QR code", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "image/png")
w.Write(qrCode)
}
func main() {
http.HandleFunc("/qr", QRCodeHandler)
http.ListenAndServe(":8080", nil)
}
Ready to Integrate with Go?
Get your free API key and start generating QR codes in your Go application.
Get Your Free API Key