Skip to content

Instantly share code, notes, and snippets.

@da-blog
Created August 4, 2023 20:46
Show Gist options
  • Save da-blog/26bc7de9bba6185261e9fb031f4e257d to your computer and use it in GitHub Desktop.
Save da-blog/26bc7de9bba6185261e9fb031f4e257d to your computer and use it in GitHub Desktop.
const TestLedgerApi = () => {
const [apiResult, setApiResult] = useState({});
const { getTokenSilently } = useAuth0();
const callLedgerApi = async () => {
try {
// Get the access token from auth0
const audienceOptions = {
audience: 'https://daml.com/ledger-api'
};
const token = await getTokenSilently(audienceOptions);
// Print the token to the console - inspect in on https://jwt.io/
console.log(token);
// Call the JSON ledger API with an attached access token
const response = await fetch("/contracts/search", {
headers: {
Authorization: `Bearer ${token}`
}
});
// Process the ledger API response
const responseData = await response.json();
setApiResult(responseData);
} catch (error) {
console.error(error);
}
};
return (
<>
<h1>Call Ledger API</h1>
<button onClick={callLedgerApi}>Fetch all contracts</button>
<p><code>{JSON.stringify(apiResult, null, 2)}</code></p>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment