Documentation
¶
Overview ¶
Package fal is a Go client for fal.ai, a 1:1 behavioral port of the official Python client (fal-client / fal_client).
The primary type is Client. Package-level functions (Run, Submit, Subscribe, Stream, Upload, ...) delegate to a lazily-initialized default Client and mirror the module-level functions of the Python SDK. Every I/O method takes a context.Context as its first argument.
Client is a thin facade: it holds a shared *transport.Core (HTTP engine, retry, CDN tokens) and an *auth.Manager (credentials), and the feature areas live in sub-packages (run, queue, stream, upload, realtime). The public surface here is flat, matching the Python client.
Index ¶
- Constants
- Variables
- func As[T any](result any) (T, error)
- func Cancel(ctx context.Context, application, requestID string) error
- func Encode(data []byte, contentType string) string
- func EncodeFile(path string) (string, error)
- func EncodeImage(img image.Image, format ImageFormat) (string, error)
- func Result(ctx context.Context, application, requestID string) (any, error)
- func Run(ctx context.Context, application string, arguments any, opts ...RunOption) (any, error)
- func Stream(ctx context.Context, application string, arguments any, opts ...StreamOption) iter.Seq2[map[string]any, error]
- func Subscribe(ctx context.Context, application string, arguments any, ...) (any, error)
- func Upload(ctx context.Context, data []byte, contentType, fileName string, ...) (string, error)
- func UploadFile(ctx context.Context, path string, opts ...UploadOption) (string, error)
- func UploadImage(ctx context.Context, img image.Image, format ImageFormat, opts ...UploadOption) (string, error)
- func WSConnect(ctx context.Context, application string, opts ...RealtimeOption) (*websocket.Conn, error)
- type AppID
- type Client
- func (c *Client) Cancel(ctx context.Context, application, requestID string) error
- func (c *Client) GetHandle(application, requestID string) (*RequestHandle, error)
- func (c *Client) Realtime(ctx context.Context, application string, opts ...RealtimeOption) (*RealtimeConnection, error)
- func (c *Client) Result(ctx context.Context, application, requestID string) (any, error)
- func (c *Client) Run(ctx context.Context, application string, arguments any, opts ...RunOption) (any, error)
- func (c *Client) Status(ctx context.Context, application, requestID string, withLogs bool) (Status, error)
- func (c *Client) Stream(ctx context.Context, application string, arguments any, opts ...StreamOption) iter.Seq2[map[string]any, error]
- func (c *Client) Submit(ctx context.Context, application string, arguments any, opts ...SubmitOption) (*RequestHandle, error)
- func (c *Client) Subscribe(ctx context.Context, application string, arguments any, ...) (any, error)
- func (c *Client) Upload(ctx context.Context, data []byte, contentType, fileName string, ...) (string, error)
- func (c *Client) UploadFile(ctx context.Context, path string, opts ...UploadOption) (string, error)
- func (c *Client) UploadImage(ctx context.Context, img image.Image, format ImageFormat, opts ...UploadOption) (string, error)
- func (c *Client) WSConnect(ctx context.Context, application string, opts ...RealtimeOption) (*websocket.Conn, error)
- type ClientOption
- type Completed
- type Error
- type HTTPError
- type ImageFormat
- type InProgress
- type MissingCredentialsError
- type ObjectExpiration
- type Queued
- type RealtimeConnection
- type RealtimeError
- type RealtimeOption
- type RequestHandle
- type RunOption
- type Status
- type StorageACL
- type StorageACLDecision
- type StorageACLRule
- type StorageSettings
- type StreamOption
- type SubmitOption
- type SubscribeOption
- type TimeoutError
- type UploadOption
Examples ¶
Constants ¶
const ( ExpireNever = storage.ExpireNever ExpireImmediate = storage.ExpireImmediate Expire1h = storage.Expire1h Expire1d = storage.Expire1d Expire7d = storage.Expire7d Expire30d = storage.Expire30d Expire1y = storage.Expire1y ACLHide = storage.ACLHide ACLForbid = storage.ACLForbid ACLAllow = storage.ACLAllow )
const ( FormatJPEG = encode.FormatJPEG FormatPNG = encode.FormatPNG )
const ( DefaultPollInterval = option.DefaultPollInterval DefaultRealtimeTokenExpiration = option.DefaultRealtimeTokenExpiration PriorityNormal = option.PriorityNormal PriorityLow = option.PriorityLow )
Defaults and priority values (see package option).
const DefaultTimeout = 120 * time.Second
DefaultTimeout is the per-request HTTP timeout used when none is configured.
const Version = "0.1.0"
Version is the module version, reported in the User-Agent header.
Variables ¶
var ( // WithPath sets the sub-path appended to the application endpoint // (Run/Submit/Subscribe/Stream/Realtime). WithPath = option.WithPath // WithHint sets the X-Fal-Runner-Hint header (Run/Submit/Subscribe). WithHint = option.WithHint // WithHeaders sets extra request headers (Run/Submit/Subscribe). WithHeaders = option.WithHeaders // WithStartTimeout sets the server-side queue wait limit (Run/Submit/Subscribe). WithStartTimeout = option.WithStartTimeout // WithTimeout sets the client-side HTTP timeout for a single call (Run/Stream). WithTimeout = option.WithTimeout // WithPriority sets the X-Fal-Queue-Priority header (Submit/Subscribe). WithPriority = option.WithPriority // WithWebhookURL registers a fal_webhook callback URL on Submit. WithWebhookURL = option.WithWebhookURL // WithLogs requests logs in status updates during Subscribe. WithLogs = option.WithLogs // WithInterval sets the queue poll interval for Subscribe. WithInterval = option.WithInterval // WithClientTimeout bounds the total Subscribe operation (returns *TimeoutError). WithClientTimeout = option.WithClientTimeout // OnEnqueue registers a callback invoked once with the request id (Subscribe). OnEnqueue = option.OnEnqueue // OnQueueUpdate registers a callback invoked on every status poll (Subscribe). OnQueueUpdate = option.OnQueueUpdate // WithRepository selects the primary upload backend ("fal_v3" or "fal"). WithRepository = option.WithRepository // WithFallbackRepository sets the ordered upload fallback backends; no args disables the default. WithFallbackRepository = option.WithFallbackRepository // WithLifecycle sets the storage lifecycle/ACL of the uploaded object. WithLifecycle = option.WithLifecycle // WithJWT toggles JWT-in-URL auth for realtime connections (default true). WithJWT = option.WithJWT // WithMaxBuffering sets the server-side realtime buffering window (1-60). WithMaxBuffering = option.WithMaxBuffering // WithTokenExpiration sets the realtime JWT expiration in seconds. WithTokenExpiration = option.WithTokenExpiration // WithEncodeMessage overrides the realtime outbound message encoder (default msgpack). WithEncodeMessage = option.WithEncodeMessage // WithDecodeMessage overrides the realtime inbound message decoder (default msgpack). WithDecodeMessage = option.WithDecodeMessage )
Functional options, re-exported from package option so callers use them as fal.WithPath, fal.WithLogs, etc. They are var bindings (not wrapper functions) because several options satisfy more than one per-method interface, which a single wrapper return type could not preserve. Each carries its own doc comment so it documents under fal.* on pkg.go.dev; see package option for the canonical definitions.
Functions ¶
func As ¶
As decodes a dynamic result (as returned by Run/Subscribe/Result) into a typed value via a JSON round-trip. It is a Go-idiomatic convenience over the Python-parity any return type.
out, err := fal.As[MyResult](result)
Example ¶
As decodes a dynamic result into a typed struct.
package main
import (
fal "github.com/valksor/fal-go"
)
func main() { //nolint:testableexamples // illustrative decode over placeholder data; no deterministic output to assert
type sdxlResult struct {
Images []struct {
URL string `json:"url"`
} `json:"images"`
}
var raw any // returned by Run/Subscribe/Result
out, err := fal.As[sdxlResult](raw)
if err != nil {
panic(err)
}
_ = out
}
Output:
func EncodeFile ¶
EncodeFile reads a file and returns a base64 data URI. See encode.EncodeFile.
func EncodeImage ¶
func EncodeImage(img image.Image, format ImageFormat) (string, error)
EncodeImage encodes an image and returns a base64 data URI. See encode.EncodeImage.
func Stream ¶
func Stream(ctx context.Context, application string, arguments any, opts ...StreamOption) iter.Seq2[map[string]any, error]
Stream calls Default().Stream. It mirrors fal_client.stream.
func Subscribe ¶
func Subscribe(ctx context.Context, application string, arguments any, opts ...SubscribeOption) (any, error)
Subscribe calls Default().Subscribe. It mirrors fal_client.subscribe.
Example ¶
Subscribe submits a request and blocks until the result is ready, reporting queue progress through a callback.
package main
import (
"context"
"fmt"
fal "github.com/valksor/fal-go"
)
func main() { //nolint:testableexamples // illustrative network call; no deterministic output to assert
ctx := context.Background()
result, err := fal.Subscribe(
ctx, "fal-ai/fast-sdxl",
map[string]any{"prompt": "a cat riding a bicycle"},
fal.WithLogs(true),
fal.OnQueueUpdate(func(s fal.Status) {
switch v := s.(type) {
case fal.Queued:
fmt.Println("queued at", v.Position)
case fal.InProgress:
fmt.Println("in progress")
case fal.Completed:
fmt.Println("done")
}
}),
)
if err != nil {
panic(err)
}
_ = result
}
Output:
func Upload ¶
func Upload(ctx context.Context, data []byte, contentType, fileName string, opts ...UploadOption) (string, error)
Upload calls Default().Upload. It mirrors fal_client.upload.
func UploadFile ¶
UploadFile calls Default().UploadFile. It mirrors fal_client.upload_file.
func UploadImage ¶
func UploadImage(ctx context.Context, img image.Image, format ImageFormat, opts ...UploadOption) (string, error)
UploadImage calls Default().UploadImage. It mirrors fal_client.upload_image.
Pass FormatJPEG or FormatPNG for an explicit call; the zero value (empty ImageFormat) defaults to JPEG.
Types ¶
type AppID ¶
Application identifier (see package appid).
func ParseAppID ¶
ParseAppID parses an endpoint id such as "fal-ai/fast-sdxl". See appid.Parse.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a fal.ai API client. The zero value is not usable; construct one with New. A Client is safe for concurrent use by multiple goroutines.
func Default ¶
func Default() *Client
Default returns the shared package-level client used by the top-level functions (Run, Submit, Subscribe, ...).
func New ¶
func New(opts ...ClientOption) *Client
New creates a Client. With no options it resolves credentials and endpoints from the environment.
func (*Client) GetHandle ¶
func (c *Client) GetHandle(application, requestID string) (*RequestHandle, error)
GetHandle reconstructs a handle from an application id and request id. It mirrors SyncClient.get_handle.
func (*Client) Realtime ¶
func (c *Client) Realtime(ctx context.Context, application string, opts ...RealtimeOption) (*RealtimeConnection, error)
Realtime opens a realtime connection to an application. It mirrors fal_client.realtime / SyncClient.realtime.
func (*Client) Result ¶
Result polls a request by id until completion and returns its result. It mirrors fal_client.result.
func (*Client) Run ¶
func (c *Client) Run(ctx context.Context, application string, arguments any, opts ...RunOption) (any, error)
Run executes an application synchronously and returns its result. It mirrors fal_client.run / SyncClient.run.
func (*Client) Status ¶
func (c *Client) Status(ctx context.Context, application, requestID string, withLogs bool) (Status, error)
Status fetches the status of a request by id. It mirrors fal_client.status.
func (*Client) Stream ¶
func (c *Client) Stream(ctx context.Context, application string, arguments any, opts ...StreamOption) iter.Seq2[map[string]any, error]
Stream opens a Server-Sent Events stream and yields each decoded JSON event. It mirrors fal_client.stream / SyncClient.stream.
Example ¶
Stream consumes Server-Sent Events; break-on-error is mandatory.
package main
import (
"context"
"fmt"
fal "github.com/valksor/fal-go"
)
func main() { //nolint:testableexamples // illustrative network stream; no deterministic output to assert
ctx := context.Background()
c := fal.New()
for event, err := range c.Stream(ctx, "fal-ai/some-streaming-app", map[string]any{}) {
if err != nil {
fmt.Println("stream error:", err)
break
}
fmt.Println(event)
}
}
Output:
func (*Client) Submit ¶
func (c *Client) Submit(ctx context.Context, application string, arguments any, opts ...SubmitOption) (*RequestHandle, error)
Submit enqueues a request and returns a handle to poll it. It mirrors fal_client.submit / SyncClient.submit.
func (*Client) Subscribe ¶
func (c *Client) Subscribe(ctx context.Context, application string, arguments any, opts ...SubscribeOption) (any, error)
Subscribe submits a request and polls until completion, returning the result. It mirrors fal_client.subscribe / SyncClient.subscribe.
func (*Client) Upload ¶
func (c *Client) Upload(ctx context.Context, data []byte, contentType, fileName string, opts ...UploadOption) (string, error)
Upload uploads raw data and returns its public URL. It mirrors fal_client.upload / SyncClient.upload.
func (*Client) UploadFile ¶
UploadFile uploads a local file and returns its public URL. It mirrors fal_client.upload_file.
func (*Client) UploadImage ¶
func (c *Client) UploadImage(ctx context.Context, img image.Image, format ImageFormat, opts ...UploadOption) (string, error)
UploadImage encodes an image and uploads it. It mirrors fal_client.upload_image.
type ClientOption ¶
type ClientOption func(*config)
ClientOption configures a Client.
func WithDefaultTimeout ¶
func WithDefaultTimeout(d time.Duration) ClientOption
WithDefaultTimeout sets the default per-request HTTP timeout.
func WithHTTPClient ¶
func WithHTTPClient(h *http.Client) ClientOption
WithHTTPClient sets the underlying *http.Client.
func WithKey ¶
func WithKey(key string) ClientOption
WithKey sets an explicit fal key, overriding environment resolution.
type ImageFormat ¶
type ImageFormat = encode.ImageFormat
Image encoding (see package encode). ImageFormat lives in encode because both EncodeImage and UploadImage take it.
type MissingCredentialsError ¶
type MissingCredentialsError = errs.MissingCredentialsError
Error types (see package errs).
type ObjectExpiration ¶
type ObjectExpiration = storage.ObjectExpiration
Storage configuration (see package storage).
type RealtimeConnection ¶
type RealtimeConnection = realtime.Connection
Queue and realtime types (see packages queue and realtime).
func Realtime ¶
func Realtime(ctx context.Context, application string, opts ...RealtimeOption) (*RealtimeConnection, error)
Realtime calls Default().Realtime. It mirrors fal_client.realtime.
type RealtimeError ¶
Queue and realtime types (see packages queue and realtime).
type RealtimeOption ¶
type RealtimeOption = option.RealtimeOption
Per-method option interfaces (see package option).
type RequestHandle ¶
Queue and realtime types (see packages queue and realtime).
func GetHandle ¶
func GetHandle(application, requestID string) (*RequestHandle, error)
GetHandle calls Default().GetHandle. It mirrors fal_client.get_handle.
It takes no context because it performs no I/O — it only parses the application id and constructs the request URLs. The returned handle's methods take a context.
func Submit ¶
func Submit(ctx context.Context, application string, arguments any, opts ...SubmitOption) (*RequestHandle, error)
Submit calls Default().Submit. It mirrors fal_client.submit.
type StorageACLDecision ¶
type StorageACLDecision = storage.ACLDecision
Storage configuration (see package storage).
type StorageACLRule ¶
Storage configuration (see package storage).
type StorageSettings ¶
Storage configuration (see package storage).
type StreamOption ¶
type StreamOption = option.StreamOption
Per-method option interfaces (see package option).
type SubmitOption ¶
type SubmitOption = option.SubmitOption
Per-method option interfaces (see package option).
type SubscribeOption ¶
type SubscribeOption = option.SubscribeOption
Per-method option interfaces (see package option).
type UploadOption ¶
type UploadOption = option.UploadOption
Per-method option interfaces (see package option).
Directories
¶
| Path | Synopsis |
|---|---|
|
Package appid parses and normalizes fal application identifiers.
|
Package appid parses and normalizes fal application identifiers. |
|
Package auth resolves fal credentials and produces the Authorization header.
|
Package auth resolves fal credentials and produces the Authorization header. |
|
Package encode produces base64 data URIs for inline file and image payloads.
|
Package encode produces base64 data URIs for inline file and image payloads. |
|
Package errs holds the SDK's error types.
|
Package errs holds the SDK's error types. |
|
Package option holds the functional options for the fal client and the resolved CallOptions struct the feature packages consume.
|
Package option holds the functional options for the fal client and the resolved CallOptions struct the feature packages consume. |
|
Package queue implements the asynchronous queue lifecycle: submit, poll, fetch result, cancel and subscribe.
|
Package queue implements the asynchronous queue lifecycle: submit, poll, fetch result, cancel and subscribe. |
|
Package realtime implements bidirectional WebSocket connections to fal apps.
|
Package realtime implements bidirectional WebSocket connections to fal apps. |
|
Package run implements direct (non-queued) application execution.
|
Package run implements direct (non-queued) application execution. |
|
Package status holds the sealed queue status type and its three cases.
|
Package status holds the sealed queue status type and its three cases. |
|
Package storage holds the upload lifecycle and access-control configuration types.
|
Package storage holds the upload lifecycle and access-control configuration types. |
|
Package stream implements Server-Sent Events streaming.
|
Package stream implements Server-Sent Events streaming. |
|
Package transport is the SDK's HTTP engine: request building (with auth and User-Agent), the retry loop, JSON decoding, and the CDN token cache.
|
Package transport is the SDK's HTTP engine: request building (with auth and User-Agent), the retry loop, JSON decoding, and the CDN token cache. |
|
Package upload implements file and image uploads to the fal storage backends, including the multipart path for large files.
|
Package upload implements file and image uploads to the fal storage backends, including the multipart path for large files. |