You can integrate your DApp seamlessly with Fuel Wallet using our simple API, which is injected directly into the browser window. This eliminates the need for additional dependencies, allowing you to perform key actions effortlessly. For even more powerful and customizable integration, use Fuel Wallet in conjunction with the Fuel TS SDK .
If you've correctly installed the Fuel wallet extension, the wallet SDK will be injected automatically on the window
object on the property fuel
. To access it, you can use window.fuel
window.fuel.connect();
You can try this code directly in the developer console.
The Fuel Wallet SDK provides a package on npm, making it more convenient to use in TypeScript projects.
To use it, you must also install the fuels
package as a dependency.
npm install fuels @fuel-wallet/sdk
To use the SDK and the React Hooks in your TypeScript project, add the following line to your tsconfig.json file:
{
"compilerOptions": {
"types": ["@fuel-wallet/sdk"]
}
}
Alternatively, you can use a TypeScript reference directive in any TypeScript file:
/// <reference types="@fuel-wallet/sdk" />
With the SDK imported, fuel
will be conveniently typed and accessible on the window
object.
window.fuel?.connect();
Alternatively, you can directly use the SDK by creating new instance of Fuel
to interact with the wallet.
import { Fuel } from '@fuel-wallet/sdk';
const fuel = new Fuel();
await fuel.connect();
It's possible that your application loads before window.fuel
is injected. To detect when the fuel
is loaded and ready to use, you can listen to the event FuelLoaded
on the document.
// Fuel loaded handler
const onFuelLoaded = () => {
setFuel(window.fuel);
};
// If fuel is already loaded, call the handler
if (window.fuel) {
onFuelLoaded();
}
// Listen for the fuelLoaded event
document.addEventListener("FuelLoaded", onFuelLoaded);
// On unmount, remove the event listener
return () => {
document.removeEventListener("FuelLoaded", onFuelLoaded);
};
Fuel Wallet provides a set of React hooks that improve the developer experience.
npm install @fuel-wallet/react
Wrap your application with the FuelProvider
component.
// index.tsx
import { FuelProvider } from '@fuel-wallet/react';
//...
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<FuelProvider>
<App />
</FuelProvider>
</React.StrictMode>
);
Here is how you can use the useFuel
hook in a component to access an instance of Fuel
:
import { useFuel } from '@fuel-wallet/react';
// ...
const { fuel } = useFuel();
await fuel.connect();