Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

UnsafeApi

The UnsafeApi enables interaction with the chain easily to the same extend TypedApi does, but it does not requires any descriptors. It is an advanced method and should only be used if you really know what you are doing.

In order to create it, you can still pass a descriptors' type to get the same type inference as in the typedApi, but the shape of the entries at runtime level is not guaranteed.

Its primary use cases are applications that make no assumptions about the current runtime (e.g., a dev console that reads the latest metadata and exposes interactions dynamically, rather than relying on hard-coded calls), or environments where generating descriptors is not possible.

The UnsafeApi has the following structure:

type UnsafeApi = {
  query: StorageApi
  tx: TxApi
  txFromCallData: TxFromBinary
  event: EvApi
  apis: RuntimeCallsApi
  constants: ConstApi
  getStaticApis: (options?: {
    at?: HexString | "finalized" | "best"
    signal?: AbortSignal
  }) => Promise<StaticApis>
}

In order to create the unsafe api, it is actually very straightforward:

const unsafeApi = client.getUnsafeApi() // without typings
 
// optionally generate descriptors to get type inference
import { dot } from "@polkadot-api/descriptors"
const unsafeApi = client.getUnsafeApi<typeof dot>() // with typings

One can notice the API is actually very similar to the TypedApi, check its docs for the API reference since it behaves the same way, except that it doesn't perform any compatibility check.