Send over QUIC

QUIC is the lowest-latency way to send. You connect once, the handshake and your identity are settled a single time, and every later send is a stream write on a connection that is already open.

Endpoint

text
quic.flashland.dev:11000   (UDP)

Use the host name above as the TLS server name (SNI); the server picks its certificate by it.

Authentication

There is no Authorization header on QUIC. Your API key decodes to an Ed25519 keypair. The client presents a self-signed client certificate carrying that key in the TLS handshake, and the server resolves your account from it once, when the connection opens. Keep the key in an environment variable; the samples below read SEND_API_KEY.

Protocol

ultrasend/1solana-tpu
ALPNultrasend/1solana-tpu
Streamone bidirectional stream per transactionone unidirectional stream per transaction
Replyone JSON object, at most 4 KiBnone (raw mode)
  1. Connect once with the ALPN you want and keep the connection open between sends.
  2. Open a stream, write the serialized signed transaction (raw bytes, no JSON, no base64), and finish the send half.
  3. With ultrasend/1, read the reply: a receipt, or an error object with the same codes as HTTPS.

The payload is at most 1,232 bytes. Balance is checked for each transaction, not at the handshake, and the same prepaid balance, price and refunds apply as over HTTPS.

The receipt

json
{
  "receipt": {
    "signature": "5Nx3...qT7w",
    "accepted": true,
    "duplicate": false,
    "charged_lamports": 200000,
    "balance_remaining_lamports": 49800000,
    "payment_mode": "balance"
  }
}

An error looks like {"error":{"code":"INSUFFICIENT_BALANCE","message":"..."}}. The codes are the ones listed in Send Transactions.

Rust

rust
use std::{net::ToSocketAddrs, sync::Arc};
use anyhow::Result;
use quinn::{crypto::rustls::QuicClientConfig, ClientConfig, Endpoint};

// The API key decodes to an Ed25519 keypair. The client presents a self-signed
// certificate carrying that key; the server resolves your account from it once,
// when the connection opens. There is no Authorization header on this path.
fn client_config(api_key: &str) -> Result<ClientConfig> {
    let keypair = solana_keypair::Keypair::try_from_base58_string(
        api_key.trim_start_matches("usq_live_"),
    )?;
    let (cert, key) = solana_tls_utils::new_dummy_x509_certificate(&keypair);

    let mut roots = rustls::RootCertStore::empty();
    roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
    let mut crypto = rustls::ClientConfig::builder_with_provider(Arc::new(
        solana_tls_utils::crypto_provider(),
    ))
    .with_safe_default_protocol_versions()?
    .with_root_certificates(roots)
    .with_client_auth_cert(vec![cert], key)?;
    crypto.alpn_protocols = vec![b"ultrasend/1".to_vec()];
    Ok(ClientConfig::new(Arc::new(QuicClientConfig::try_from(crypto)?)))
}

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = std::env::var("SEND_API_KEY")?;
    let addr = "quic.flashland.dev:11000".to_socket_addrs()?.next().unwrap();

    let mut endpoint = Endpoint::client("0.0.0.0:0".parse()?)?;
    endpoint.set_default_client_config(client_config(&api_key)?);

    // Connect once and keep the connection for the life of the process.
    let conn = endpoint.connect(addr, "quic.flashland.dev")?.await?;

    let tx_bytes: Vec<u8> = next_signed_transaction().await?;
    let (mut send, mut recv) = conn.open_bi().await?;   // one stream per transaction
    send.write_all(&tx_bytes).await?;
    send.finish()?;                                      // finish the send half
    let reply = recv.read_to_end(4096).await?;
    println!("{}", String::from_utf8_lossy(&reply));
    Ok(())
}

Go

go
package main

import (
	"context"
	"crypto/ed25519"
	"crypto/rand"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"io"
	"log"
	"math/big"
	"os"
	"strings"
	"time"

	"github.com/mr-tron/base58"
	"github.com/quic-go/quic-go"
)

// A self-signed certificate carrying the keypair the API key decodes to.
// The server resolves your account from it once, when the connection opens.
func clientTLS(apiKey string) (*tls.Config, error) {
	raw, err := base58.Decode(strings.TrimPrefix(apiKey, "usq_live_"))
	if err != nil {
		return nil, err
	}
	priv := ed25519.PrivateKey(raw)
	tmpl := &x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject:      pkix.Name{CommonName: "client"},
		NotBefore:    time.Now().Add(-time.Hour),
		NotAfter:     time.Now().Add(24 * 365 * time.Hour),
	}
	der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, priv.Public(), priv)
	if err != nil {
		return nil, err
	}
	return &tls.Config{
		Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: priv}},
		NextProtos:   []string{"ultrasend/1"},
		ServerName:   "quic.flashland.dev", // the server's certificate is verified for this name
	}, nil
}

func main() {
	tlsConf, err := clientTLS(os.Getenv("SEND_API_KEY"))
	if err != nil {
		log.Fatal(err)
	}
	// Connect once and keep the connection.
	conn, err := quic.DialAddr(context.Background(), "quic.flashland.dev:11000", tlsConf,
		&quic.Config{KeepAlivePeriod: 25 * time.Second, MaxIdleTimeout: 5 * time.Minute})
	if err != nil {
		log.Fatal(err)
	}

	txBytes := nextSignedTransaction()
	stream, err := conn.OpenStreamSync(context.Background()) // one stream per transaction
	if err != nil {
		log.Fatal(err)
	}
	stream.Write(txBytes)
	stream.Close() // finish the send half
	body, _ := io.ReadAll(io.LimitReader(stream, 4096))
	log.Println(string(body))
}

Troubleshooting

  • Certificate or name error: connect to the host name above, not an IP address, and use it as the server name.
  • Connection closed right after the handshake: the key is unknown or the account is switched off.
  • Connection times out: UDP port 11000 must be reachable from your network; some corporate networks block UDP.
  • Reconnecting on every send: you lose the speed advantage. Keep the connection and open a new stream per transaction.

Pay the tip inside the transaction

The in-transaction tip works the same over QUIC: add the transfer to the tip account YOUR_TIP_ADDRESS (at least 0.0002 SOL, no maximum) to the transaction you sign. See Send Transactions.