Skip to main content

decode

Decodes the response from 'fcl.send()' into the appropriate JSON representation of any values returned from Cadence code.

The response from Flow contains encoded values that need to be decoded into JavaScript types. This function handles that conversion, including complex types like structs, arrays, and dictionaries.

Import

You can import the entire package and access the function:


_10
import * as sdk from "@onflow/sdk"
_10
_10
sdk.decode(response)

Or import directly the specific function:


_10
import { decode } from "@onflow/sdk"
_10
_10
decode(response)

Usage


_27
import * as fcl from "@onflow/fcl";
_27
_27
// Simple script to add 2 numbers
_27
const response = await fcl.send([
_27
fcl.script`
_27
access(all) fun main(int1: Int, int2: Int): Int {
_27
return int1 + int2
_27
}
_27
`,
_27
fcl.args([fcl.arg(1, fcl.t.Int), fcl.arg(2, fcl.t.Int)])
_27
]);
_27
_27
const decoded = await fcl.decode(response);
_27
console.log(decoded); // 3
_27
console.log(typeof decoded); // "number"
_27
_27
// Complex return types
_27
const complexResponse = await fcl.send([
_27
fcl.script`
_27
access(all) fun main(): {String: Int} {
_27
return {"foo": 1, "bar": 2}
_27
}
_27
`
_27
]);
_27
_27
const complexDecoded = await fcl.decode(complexResponse);
_27
console.log(complexDecoded); // {foo: 1, bar: 2}

Parameters

response

  • Type: any
  • Description: Should be the response returned from 'fcl.send([...])'

Returns

Promise<any>


Rate this page