NextBlock
SupportWebsiteDashboard
  • What is NextBlock?
  • Pricing & Rate Limits
  • Getting Started
    • Quickstart
      • GoLang Sample Code
      • Bundle Sending Sample
    • Contact
  • API
    • Basics
    • Authentication
    • Submit Transaction
    • Submit Batched Transactions
    • Examples
      • Golang
        • Connection
        • Keepalive
        • Tip floor stream
        • Submit single transactions
        • Submit batched transactions
      • Http
      • Rust
Powered by GitBook
On this page
Export as PDF
  1. API
  2. Examples
  3. Golang

Connection

Establish the connection, either insecure (faster) or secured by passing plaintext flag.

import (
  "context"
  "time"
  "crypto/x509"
  "google.golang.org/grpc"
  "google.golang.org/grpc/credentials"
  "google.golang.org/grpc/credentials/insecure"
  "google.golang.org/grpc/keepalive"
)

type ApiKeyCredentials struct {}

func (a ApiKeyCredentials) RequireTransportSecurity() bool {
	return false
}

func (a ApiKeyCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
	return map[string]string{
		"authorization": "<your api key here>",
	}, nil
}

var kacp = keepalive.ClientParameters{
	Time:                time.Minute,
	Timeout:             15 * time.Second,
	PermitWithoutStream: true,
}

func connectToNextblock(address string, plaintext bool) *grpc.ClientConn {
	var opts []grpc.DialOption
	if plaintext {
		opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
	} else {
		pool, _ := x509.SystemCertPool()
		creds := credentials.NewClientTLSFromCert(pool, "")
		opts = append(opts, grpc.WithTransportCredentials(creds))
	}

	opts = append(opts, grpc.WithKeepaliveParams(kacp))
	opts = append(opts, grpc.WithPerRPCCredentials(&ApiKeyCredentials{}))
	conn, err := grpc.NewClient(address, opts...)
	if err != nil {
		return nil
	}
	return conn
}

Pass the connection to the proto generated api client

nextblockApiClient := NewApiClient(con)

PreviousGolangNextKeepalive

Last updated 6 days ago