|
| 1 | +import { Step, Steps, DocImage, Callout } from "@doc"; |
| 2 | +import NewProject from "../assets/new-project.png"; |
| 3 | +import KeysSetup from "../assets/keys.png"; |
| 4 | + |
| 5 | +# Get Started |
| 6 | + |
| 7 | +Learn how to get set up and started with the Nebula API to successfully prepare and enable a connected user to sign a transfer . |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Before you begin, ensure you have the following: |
| 12 | + |
| 13 | +- A thirdweb account |
| 14 | +- A blockchain wallet for executing transactions |
| 15 | +- Node.js and npm or yarn installed on your system |
| 16 | + |
| 17 | +## Obtain Client ID & Secret Key |
| 18 | + |
| 19 | +<Steps> |
| 20 | + |
| 21 | +<Step title="Create project"> |
| 22 | + |
| 23 | +Navigate to the [projects dashboard](https://thirdweb.com/) and create a new project. |
| 24 | + |
| 25 | +<DocImage src={NewProject} alt="Create a new project"/> |
| 26 | + |
| 27 | +</Step> |
| 28 | + |
| 29 | +<Step title="Obtain keys"> |
| 30 | + |
| 31 | +Setup your project and obtain your client ID and secret key. Please note your secret key somewhere safe as it is not recoverable. |
| 32 | + |
| 33 | +<Callout variant="warning" title="Client Id vs Secret Key"> |
| 34 | +Client Id is used for client side usage and is restricted by the domain restrictions you set on your API key, it is a public identifier which can be used on the frontend safely. |
| 35 | + |
| 36 | +Secret key is used for server side or script usage and is not restricted by the domain restrictions. Never expose your secret key in client side code. |
| 37 | +</Callout> |
| 38 | + |
| 39 | +<DocImage src={KeysSetup} alt="Obtain keys"/> |
| 40 | + |
| 41 | +</Step> |
| 42 | + |
| 43 | +</Steps> |
| 44 | + |
| 45 | +## Setup API (TypeScript SDK) |
| 46 | + |
| 47 | +<Steps> |
| 48 | + |
| 49 | +<Step title="Install SDK"> |
| 50 | + |
| 51 | +Install the TypeScript SDK |
| 52 | + |
| 53 | +```bash |
| 54 | +npm install thirdweb |
| 55 | +``` |
| 56 | +</Step> |
| 57 | + |
| 58 | +<Step title="Environment Variables"> |
| 59 | + |
| 60 | +Setup environmental variables. |
| 61 | + |
| 62 | +<Callout variant="warning" title="Storing Secret Keys"> |
| 63 | + Ensure you keep your secret key and private key safe and do not expose it in your codebase. We recommend using a |
| 64 | + secret key manager such as [AWS Secret Manager](https://aws.amazon.com/secrets-manager/) or [Google Secret Manager](https://cloud.google.com/secret-manager). |
| 65 | +</Callout> |
| 66 | + |
| 67 | +```jsx |
| 68 | +THIRDWEB_SECRET_KEY=your_thirdweb_secret_key |
| 69 | +EOA_PRIVATE_KEY=your_wallet_private_key |
| 70 | +``` |
| 71 | +</Step> |
| 72 | + |
| 73 | +<Step title="Import Libraries"> |
| 74 | + |
| 75 | +Import required libraries from thirdweb. |
| 76 | + |
| 77 | +```jsx |
| 78 | +import { |
| 79 | + createThirdwebClient, |
| 80 | + prepareTransaction, |
| 81 | + sendTransaction, |
| 82 | + privateKeyToAccount, |
| 83 | +} from "thirdweb"; |
| 84 | +``` |
| 85 | +</Step> |
| 86 | + |
| 87 | +<Step title="Create Function to Handle Response"> |
| 88 | +This function processes the API's response and executes blockchain transactions. |
| 89 | + |
| 90 | +```jsx |
| 91 | +async function handleNebulaResponse(response) { |
| 92 | + const client = createThirdwebClient({ |
| 93 | + secretKey: process.env.THIRDWEB_SECRET_KEY, |
| 94 | + }); |
| 95 | + |
| 96 | + const account = privateKeyToAccount({ |
| 97 | + client, |
| 98 | + privateKey: process.env.EOA_PRIVATE_KEY, |
| 99 | + }); |
| 100 | + |
| 101 | + if (response.actions && response.actions.length > 0) { |
| 102 | + const action = response.actions[0]; |
| 103 | + const txData = JSON.parse(action.data); |
| 104 | + |
| 105 | + try { |
| 106 | + const transaction = prepareTransaction({ |
| 107 | + to: txData.to, |
| 108 | + data: txData.data, |
| 109 | + value: BigInt(txData.value), |
| 110 | + chain: txData.chainId, |
| 111 | + client, |
| 112 | + }); |
| 113 | + |
| 114 | + const result = await sendTransaction({ |
| 115 | + transaction, |
| 116 | + account, |
| 117 | + }); |
| 118 | + |
| 119 | + console.log("Transaction Successful:", result); |
| 120 | + return result; |
| 121 | + } catch (error) { |
| 122 | + console.error("Error executing transaction:", error); |
| 123 | + throw error; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | +</Step> |
| 129 | + |
| 130 | +<Step title="Call Nebula API"> |
| 131 | + |
| 132 | +Send a request to the Nebula API to interpret your natural language command and retrieve the transaction details. |
| 133 | + |
| 134 | +```jsx |
| 135 | +const response = await fetch("https://nebula-api.thirdweb.com/chat", { |
| 136 | + method: "POST", |
| 137 | + headers: { |
| 138 | + "Content-Type": "application/json", |
| 139 | + "x-secret-key": process.env.THIRDWEB_SECRET_KEY, |
| 140 | + }, |
| 141 | + body: JSON.stringify({ |
| 142 | + message: "send 0.001 ETH on Sepolia to vitalik.eth", |
| 143 | + execute_config: { |
| 144 | + mode: "client", |
| 145 | + signer_wallet_address: "0xYourWalletAddress", |
| 146 | + }, |
| 147 | + }), |
| 148 | +}); |
| 149 | + |
| 150 | +const data = await response.json(); |
| 151 | +await handleNebulaResponse(data); |
| 152 | +``` |
| 153 | +</Step> |
| 154 | + |
| 155 | +<Step title="Example Response"> |
| 156 | + |
| 157 | +The response from the API will contain the transaction details. |
| 158 | + |
| 159 | +```jsx |
| 160 | +Transaction Successful: { |
| 161 | + transactionHash: "0x123abc...", |
| 162 | + blockNumber: 1234567, |
| 163 | + ... |
| 164 | +} |
| 165 | +``` |
| 166 | +</Step> |
| 167 | + |
| 168 | +Congratulations! You have successfully set up the Nebula API and executed a transaction using the thirdweb SDK. |
| 169 | +</Steps> |
| 170 | + |
| 171 | +### Additional Resources |
| 172 | + |
| 173 | +- [Nebula API Documentation](https://portal.thirdweb.com/nebula/api-reference) |
| 174 | +- [thirdweb SDK Documentation](https://portal.thirdweb.com/typescript/v5) |
0 commit comments