{"record":{"id":"5f861dab3e07536f","repo":"ipfs/kubo","slug":"cannot-apply-options-to-api-without-node","errorCode":null,"errorMessage":"cannot apply options to api without node","messagePattern":"cannot apply options to api without node","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/coreapi/coreapi.go","lineNumber":158,"sourceCode":"func (api *CoreAPI) PubSub() coreiface.PubSubAPI {\n\treturn (*PubSubAPI)(api)\n}\n\n// Routing returns the RoutingAPI interface implementation backed by the kubo node\nfunc (api *CoreAPI) Routing() coreiface.RoutingAPI {\n\treturn (*RoutingAPI)(api)\n}\n\n// WithOptions returns api with global options applied\nfunc (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, error) {\n\tsettings := api.parentOpts // make sure to copy\n\t_, err := options.ApiOptionsTo(&settings, opts...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif api.nd == nil {\n\t\treturn nil, errors.New(\"cannot apply options to api without node\")\n\t}\n\n\tn := api.nd\n\n\tsubAPI := &CoreAPI{\n\t\tnctx: n.Context(),\n\n\t\tidentity:   n.Identity,\n\t\tprivateKey: n.PrivateKey,\n\n\t\trepo:       n.Repo,\n\t\tblockstore: n.Blockstore,\n\t\tbaseBlocks: n.BaseBlocks,\n\t\tpinning:    n.Pinning,\n\n\t\tblocks:               n.Blocks,\n\t\tdag:                  n.DAG,\n\t\tipldFetcherFactory:   n.IPLDFetcherFactory,","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/coreapi/coreapi.go#L140-L176","documentation":"NewCoreAPI returns a CoreAPI whose node may be unset; WithOptions copies the API with additional per-request options. Because options can affect node-backed behavior, applying them requires a node to exist. If api.nd is nil, this error is returned. Callers typically hit it when they construct a CoreAPI via the constructor that defers node binding and then call WithOptions before injecting the node.","triggerScenarios":"Calling api.WithOptions(...) on a CoreAPI created without a node (nd == nil), e.g. a CoreAPI built for testing or one obtained before core.NewNode assignment.","commonSituations":"Library users embedding kubo-as-a-library who construct coreapi.NewCoreAPI(nil-ish contexts) and apply options before node creation; test harnesses that partially initialize the API; ordering mistakes in custom node bootstrap code.","solutions":["Ensure the node is created and attached to the CoreAPI before calling WithOptions (create node first, then api.WithOptions)","Use the standard kubo-as-a-library pattern: build the IpfsNode, then obtain the CoreAPI from it","If you only need option validation, call options.ApiOptionsTo directly instead of WithOptions","Check that the code path assigning api.nd actually ran (no early-return or error swallowed)"],"exampleFix":"// before\napi := coreapi.NewCoreAPI(nil)\napi2, err := api.WithOptions(opts...)\n// after\nnd, err := core.NewNode(ctx, &core.BuildCfg{})\napi, err := coreapi.NewCoreAPI(nd)\napi2, err := api.WithOptions(opts...)","handlingStrategy":"validation","validationCode":"if api == nil || api.nd == nil {\n    return errors.New(\"CoreAPI has no node attached; create the IpfsNode first\")\n}\n_, err := api.WithOptions(opts...)","typeGuard":null,"tryCatchPattern":"api2, err := api.WithOptions(opts...)\nif err != nil && strings.Contains(err.Error(), \"cannot apply options to api without node\") {\n    return fmt.Errorf(\"init order bug: build node before applying options: %w\", err)\n}\nreturn api2, err","preventionTips":["Follow the kubo-as-a-library order: core.NewNode then coreapi/NewCoreAPI then WithOptions","Never construct CoreAPI against a nil node in production code","Add an assertion right after node creation in bootstrap code"],"tags":["coreapi","node-lifecycle","library-usage"],"backgroundTag":"missing-dependency-node","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}