hyphen

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 4 Imported by: 0

README

Hyphen Go SDK

Go Reference Go Report Card

The Hyphen Go SDK is a Go library that allows developers to easily integrate Hyphen's feature flag service Toggle, secret management service ENV, and geo information service Net Info into their Go applications.

Table of Contents

Installation

To install the Hyphen Go SDK, you can use go get:

go get github.com/Hyphen/go-sdk

Basic Usage with Hyphen

There are many ways to use the Hyphen Go SDK. To get started, you can create an instance of the Client using functional options:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Hyphen/go-sdk"
)

func main() {
	client, err := hyphen.New(
		hyphen.WithPublicAPIKey("your_public_api_key"),
		hyphen.WithApplicationID("your_application_id"),
	)
	if err != nil {
		log.Fatal(err)
	}

	result := client.Toggle.GetBoolean(context.Background(), "hyphen-sdk-boolean", false, nil)
	fmt.Printf("Boolean toggle value: %v\n", result)
}

You can also use context for more advanced targeting:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Hyphen/go-sdk"
)

func main() {
	ctx := &hyphen.ToggleContext{
		TargetingKey: "user-123",
		IPAddress:    "203.0.113.42",
		CustomAttributes: hyphen.ToggleCustomAttrs{
			"subscriptionLevel": "premium",
			"region":            "us-east",
		},
		User: &hyphen.ToggleUser{
			ID:    "user-123",
			Email: "john.doe@example.com",
			Name:  "John Doe",
			CustomAttributes: hyphen.ToggleCustomAttrs{
				"role": "admin",
			},
		},
	}

	client, err := hyphen.New(
		hyphen.WithPublicAPIKey("your_public_api_key"),
		hyphen.WithApplicationID("your_application_id"),
		hyphen.WithDefaultContext(ctx),
	)
	if err != nil {
		log.Fatal(err)
	}

	result := client.Toggle.GetBoolean(context.Background(), "hyphen-sdk-boolean", false, nil)
	fmt.Printf("Boolean toggle value: %v\n", result)
}

Toggle - Feature Flag Service

Toggle is our feature flag service that allows you to control the rollout of new features to your users.

Toggle Options
Option Description
WithPublicAPIKey(key) The public API key for your Hyphen project. Must start with "public_".
WithApplicationID(id) The application ID for your Hyphen project.
WithEnvironment(env) The environment for your Hyphen project (e.g., "production"). Defaults to "development".
WithDefaultContext(ctx) The default context to use when one is not passed to getter methods.
WithHorizonURLs(urls) Array of Horizon endpoint URLs for load balancing and failover.
WithDefaultTargetingKey(key) Default targeting key to use if one cannot be derived from context.
Toggle API
Creating a Toggle Client
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Hyphen/go-sdk"
)

func main() {
	toggle, err := hyphen.NewToggle(
		hyphen.WithPublicAPIKey("your_public_api_key"),
		hyphen.WithApplicationID("your_application_id"),
		hyphen.WithEnvironment("production"),
	)
	if err != nil {
		log.Fatal(err)
	}

	enabled := toggle.GetBoolean(context.Background(), "my-feature", false, nil)
	fmt.Printf("Feature enabled: %v\n", enabled)
}
Getting Toggle Values
// Get a boolean toggle
enabled := toggle.GetBoolean(ctx, "feature-flag", false, nil)

// Get a string toggle
message := toggle.GetString(ctx, "welcome-message", "Hello!", nil)

// Get a number toggle
maxRetries := toggle.GetNumber(ctx, "max-retries", 3.0, nil)

// Get an object toggle
config := toggle.GetObject(ctx, "app-config", map[string]interface{}{"theme": "light"}, nil)
Context Override

You can override the context for a single request:

overrideContext := &hyphen.ToggleContext{
	TargetingKey: "user-456",
	User: &hyphen.ToggleUser{
		ID:    "user-456",
		Email: "jane.doe@example.com",
	},
}

result := toggle.GetBoolean(ctx, "feature-flag", false, overrideContext)
Toggle Error Handling

The SDK provides error handling through a callback mechanism:

toggle, err := hyphen.NewToggle(
	hyphen.WithPublicAPIKey("your_public_api_key"),
	hyphen.WithApplicationID("your_application_id"),
)
if err != nil {
	panic(err)
}

// Set error handler
toggle.SetErrorHandler(func(err error) {
	fmt.Printf("Toggle error: %v\n", err)
})

// When an error occurs, the handler will be called and the default value will be returned
result := toggle.GetBoolean(ctx, "feature-flag", false, nil)
Toggle Environment Variables

You can use environment variables to set the PublicAPIKey and ApplicationID:

export HYPHEN_PUBLIC_API_KEY=your_public_api_key
export HYPHEN_APPLICATION_ID=your_application_id

The SDK will automatically check for these environment variables during initialization.

Toggle Self-Hosted

If you are using a self-hosted version of Hyphen, you can use the WithHorizonURLs option:

toggle, err := hyphen.NewToggle(
	hyphen.WithPublicAPIKey("your_public_api_key"),
	hyphen.WithApplicationID("your_application_id"),
	hyphen.WithHorizonURLs([]string{"https://your-self-hosted-horizon-url"}),
)

ENV - Secret Management Service

Hyphen's secret management service ENV allows you to manage your environment variables in a secure way.

Loading Environment Variables
package main

import (
	"github.com/Hyphen/go-sdk"
)

func main() {
	// Load default .env files
	err := hyphen.LoadEnv(nil)
	if err != nil {
		panic(err)
	}

	// Or with options
	err = hyphen.LoadEnv(&hyphen.EnvOptions{
		Path:        "/path/to/your/env/files/",
		Environment: "development",
		Local:       true,
	})
	if err != nil {
		panic(err)
	}
}

The loading order is:

.env -> .env.local -> .env.<environment> -> .env.<environment>.local

Net Info - Geo Information Service

The Hyphen Go SDK provides a NetInfo client for fetching geo information about IP addresses.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Hyphen/go-sdk"
)

func main() {
	netInfo, err := hyphen.NewNetInfo(
		hyphen.WithAPIKey("your_api_key"),
	)
	if err != nil {
		log.Fatal(err)
	}

	// Get info for a single IP
	ipInfo, err := netInfo.GetIPInfo(context.Background(), "8.8.8.8")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("IP Info: %+v\n", ipInfo)

	// Get info for multiple IPs
	ips := []string{"8.8.8.8", "1.1.1.1"}
	ipInfos, err := netInfo.GetIPInfos(context.Background(), ips)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("IP Infos: %+v\n", ipInfos)
}

The Hyphen Go SDK provides a Link client for creating and managing short codes and QR codes.

Creating a Short Code
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Hyphen/go-sdk"
)

func main() {
	link, err := hyphen.NewLink(
		hyphen.WithAPIKey("your_api_key"),
		hyphen.WithOrganizationID("your_organization_id"),
	)
	if err != nil {
		log.Fatal(err)
	}

	shortCode, err := link.CreateShortCode(
		context.Background(),
		"https://hyphen.ai",
		"test.h4n.link",
		&hyphen.CreateShortCodeOptions{
			Title: "My Short Code",
			Tags:  []string{"sdk-test"},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Short Code: %+v\n", shortCode)
}
// Get a short code
shortCode, err := link.GetShortCode(ctx, "code_1234567890")

// Update a short code
updated, err := link.UpdateShortCode(ctx, "code_1234567890", &hyphen.UpdateShortCodeOptions{
	Title: "Updated Title",
})

// Delete a short code
err = link.DeleteShortCode(ctx, "code_1234567890")

// Create a QR code
qrCode, err := link.CreateQRCode(ctx, "code_1234567890", &hyphen.CreateQRCodeOptions{
	Title: "My QR Code",
	Size:  hyphen.QRSizeMedium,
})

// Get QR codes for a short code
qrCodes, err := link.GetQRCodes(ctx, "code_1234567890", 1, 10)

// Delete a QR code
err = link.DeleteQRCode(ctx, "code_1234567890", "qr_1234567890")

All Available Options

The SDK uses a unified functional options pattern. Here are all available options:

Option Used By Description
WithAPIKey(key) Link, NetInfo API key for authentication
WithPublicAPIKey(key) Toggle Public API key (must start with "public_")
WithApplicationID(id) Toggle Application ID
WithEnvironment(env) Toggle Environment name (defaults to "development")
WithOrganizationID(id) Link Organization ID
WithDefaultContext(ctx) Toggle Default evaluation context
WithHorizonURLs(urls) Toggle Custom Horizon endpoint URLs
WithDefaultTargetingKey(key) Toggle Default targeting key
WithNetInfoBaseURI(uri) NetInfo Custom base URI
WithLinkURIs(uris) Link Custom Link service URIs

Contributing

We welcome contributions to the Hyphen Go SDK! If you have an idea for a new feature, bug fix, or improvement, please follow these steps:

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Make your changes and commit them with a clear message
  4. Push your changes to your forked repository
  5. Create a pull request to the main repository

Testing

To run the tests:

go test ./...

To run tests with coverage:

go test -cover ./...

This project is licensed under the MIT License. See the LICENSE file for details.

The copyright for this project is held by Hyphen, Inc. All rights reserved.

Documentation

Overview

Package hyphen provides a Go SDK for the Hyphen platform services including Toggle (feature flags), NetInfo (geo information), and Link (URL shortening).

Index

Constants

View Source
const (
	QRSizeSmall  = link.QRSizeSmall
	QRSizeMedium = link.QRSizeMedium
	QRSizeLarge  = link.QRSizeLarge
)

Re-export constants

Variables

View Source
var LoadEnv = env.LoadEnv

LoadEnv loads environment variables from .env files

Functions

func NewLink(options ...Option) (*link.Link, error)

NewLink creates a new Link client

func NewNetInfo

func NewNetInfo(options ...Option) (*netinfo.NetInfo, error)

NewNetInfo creates a new NetInfo client

func NewToggle

func NewToggle(options ...Option) (*toggle.Toggle, error)

NewToggle creates a new Toggle client

Types

type Client

type Client struct {
	Toggle  *toggle.Toggle
	NetInfo *netinfo.NetInfo
	Link    *link.Link
	// contains filtered or unexported fields
}

Client is the main Hyphen SDK client that provides access to all services

func New

func New(options ...Option) (*Client, error)

New creates a new Hyphen client with all services configured

type CreateQRCodeOptions

type CreateQRCodeOptions = link.CreateQRCodeOptions

Re-export main types for convenience

type CreateShortCodeOptions

type CreateShortCodeOptions = link.CreateShortCodeOptions

Re-export main types for convenience

type EnvOptions

type EnvOptions = env.EnvOptions

EnvOptions for environment variable loading

type GetCodeStatsResponse

type GetCodeStatsResponse = link.GetCodeStatsResponse

Re-export main types for convenience

type GetQRCodesResponse

type GetQRCodesResponse = link.GetQRCodesResponse

Re-export main types for convenience

type GetShortCodesResponse

type GetShortCodesResponse = link.GetShortCodesResponse

Re-export main types for convenience

type IPInfo

type IPInfo = netinfo.IPInfo

Re-export main types for convenience

type Link = link.Link

Link types

type NetInfo

type NetInfo = netinfo.NetInfo

NetInfo types

type Option

type Option func(*Options)

Option is a functional option for configuring Hyphen services

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey sets the API key (used by Link and NetInfo services)

func WithApplicationID

func WithApplicationID(id string) Option

WithApplicationID sets the application ID (used by Toggle service)

func WithDefaultContext

func WithDefaultContext(ctx *toggle.Context) Option

WithDefaultContext sets the default context for Toggle evaluations

func WithDefaultTargetingKey

func WithDefaultTargetingKey(key string) Option

WithDefaultTargetingKey sets the default targeting key for Toggle

func WithEnvironment

func WithEnvironment(env string) Option

WithEnvironment sets the environment (used by Toggle service)

func WithHorizonURLs

func WithHorizonURLs(urls []string) Option

WithHorizonURLs sets custom Horizon URLs for the Toggle service

func WithLinkURIs

func WithLinkURIs(uris []string) Option

WithLinkURIs sets custom URIs for the Link service

func WithNetInfoBaseURI

func WithNetInfoBaseURI(uri string) Option

WithNetInfoBaseURI sets the base URI for the NetInfo service

func WithOrganizationID

func WithOrganizationID(id string) Option

WithOrganizationID sets the organization ID (used by Link service)

func WithPublicAPIKey

func WithPublicAPIKey(key string) Option

WithPublicAPIKey sets the public API key (used by Toggle service)

type Options

type Options struct {
	// Common authentication
	APIKey       string // API key for Link and NetInfo services
	PublicAPIKey string // Public API key for Toggle service

	// Toggle options
	ApplicationID       string          // Application ID for Toggle
	Environment         string          // Environment for Toggle (defaults to "development")
	DefaultContext      *toggle.Context // Default context for Toggle evaluations
	HorizonURLs         []string        // Custom Horizon URLs for Toggle
	DefaultTargetingKey string          // Default targeting key for Toggle

	// NetInfo options
	NetInfoBaseURI string // Base URI for NetInfo service

	// Link options
	OrganizationID string   // Organization ID for Link service
	LinkURIs       []string // Custom URIs for Link service
}

Options contains all configuration options for Hyphen services

type QRCodeResponse

type QRCodeResponse = link.QRCodeResponse

Re-export main types for convenience

type ShortCodeResponse

type ShortCodeResponse = link.ShortCodeResponse

Re-export main types for convenience

type Toggle

type Toggle = toggle.Toggle

Toggle types

type ToggleContext

type ToggleContext = toggle.Context

Re-export main types for convenience

type ToggleCustomAttrs

type ToggleCustomAttrs = toggle.CustomAttributes

Re-export main types for convenience

type ToggleUser

type ToggleUser = toggle.User

Re-export main types for convenience

type UpdateShortCodeOptions

type UpdateShortCodeOptions = link.UpdateShortCodeOptions

Re-export main types for convenience

Directories

Path Synopsis
examples
complete command
link command
netinfo command
toggle command
internal
pkg
env
tests

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL