|
| 1 | +### Send Message |
| 2 | + |
| 3 | +```http |
| 4 | +POST /chat |
| 5 | +``` |
| 6 | + |
| 7 | +**Request Body:** |
| 8 | +```json |
| 9 | +{ |
| 10 | + "message": "Find the last 5 blocks", |
| 11 | + "session_id": "abc", |
| 12 | + "stream": true, |
| 13 | + "context_filter": { |
| 14 | + "chain_ids": [137], |
| 15 | + "contract_addresses": ["0x..."], |
| 16 | + "wallet_addresses": ["0x..."] |
| 17 | + }, |
| 18 | + "execute_config": { |
| 19 | + "mode": "client", |
| 20 | + "signer_wallet_address": "0x..." |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +**Request Parameters:** |
| 26 | + |
| 27 | +- `message` (required) |
| 28 | + - Type: string |
| 29 | + - Description: The user's input message or command to be processed by Nebula |
| 30 | + |
| 31 | +- `session_id` (optional) |
| 32 | + - Type: string |
| 33 | + - Description: Identifier for maintaining conversation context |
| 34 | + - Default: A new session will be created if omitted |
| 35 | + |
| 36 | +- `stream` (optional) |
| 37 | + - Type: boolean |
| 38 | + - Description: Controls whether the response is streamed or returned as a single response |
| 39 | + - Default: false |
| 40 | + |
| 41 | +- `context_filter` (optional) |
| 42 | + - Type: object |
| 43 | + - Description: Controls which blockchain data sources are used for context |
| 44 | + - Properties: |
| 45 | + - `chain_ids`: Array of numbers representing blockchain network IDs |
| 46 | + - `contract_addresses`: Array of strings containing contract addresses to focus on |
| 47 | + |
| 48 | +- `execute_config` (optional) |
| 49 | + - Type: object |
| 50 | + - Description: Configuration for transaction execution |
| 51 | + - Properties: |
| 52 | + - `mode`: String indicating execution mode (currently only "client" is supported) |
| 53 | + - `signer_wallet_address`: String containing the wallet address that will sign transactions |
| 54 | + |
| 55 | + #### Chat Messages |
| 56 | + |
| 57 | +Chat messages are natural language responses from Nebula. They appear in the `message` field of the response and provide formatted information, explanations, or answers to your queries. Messages can include formatted text, blockchain data, and technical details. |
| 58 | + |
| 59 | +**Example Response with Chat Message:** |
| 60 | +```json |
| 61 | +{ |
| 62 | + "message": "The last block on the Arbitrum mainnet is block number **284204124**. Here are the details:\n\n- **Block Hash:** 0xf42e3d624ae1e3fd6b89d4680f39943eb1cd3b8f0606918ef818d3021b7724f1\n- **Parent Hash:** 0x4c45cd0964281833b070b633980d8f530debdd21dfbdbf6eddf96cc93cbaac8e\n- **Timestamp:** 1734063299\n- **Gas Used:** 5,064,851\n- **Gas Limit:** 1,125,899,906,842,624\n- **Base Fee per Gas:** 10,000,000\n- **Transaction Count:** 7\n- **Withdrawals Count:** 0\n\nIf you need any more information about this block or related transactions, feel free to ask!", |
| 63 | + "actions": [], |
| 64 | + "session_id": "5d579903-5a63-434f-8667-788adfae9304", |
| 65 | + "request_id": "d46cfb80-de6a-48a6-9a97-746e1708d066" |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Response properties: |
| 70 | +- `message`: A formatted string containing the response, which may include: |
| 71 | + - Markdown formatting for better readability |
| 72 | + - Technical data (hashes, addresses, numbers) |
| 73 | + - Structured information about blockchain state |
| 74 | +- `actions`: Array of actions (empty when no transactions are needed) |
| 75 | +- `session_id`: Unique identifier for the current session |
| 76 | +- `request_id`: Unique identifier for the specific request |
| 77 | + |
| 78 | +#### Chat Actions |
| 79 | + |
| 80 | +Chat actions represent blockchain transactions or operations that Nebula has prepared in response to your request. The response includes both a detailed explanation in the `message` field and the actual transaction data in the `actions` array. |
| 81 | + |
| 82 | +**Example Response with Chat Action:** |
| 83 | +```json |
| 84 | +{ |
| 85 | + "message": "The transaction to transfer 0.0001 ETH to the address resolved from the ENS name `vitalik.eth` (which is `0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045`) is set up successfully. The simulation indicates that the transaction is likely to succeed.\n\nPlease proceed by signing and confirming the transaction.", |
| 86 | + "actions": [ |
| 87 | + { |
| 88 | + "session_id": "437a0df7-d512-4ef4-95b5-6168ccbbe097", |
| 89 | + "request_id": "c2b51ed6-da79-49ac-b411-206a42059509", |
| 90 | + "type": "sign_transaction", |
| 91 | + "source": "executor", |
| 92 | + "data": "{\"chainId\": 11155111, \"to\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\", \"data\": \"0x\", \"value\": \"0x5af3107a4000\"}" |
| 93 | + } |
| 94 | + ], |
| 95 | + "session_id": "437a0df7-d512-4ef4-95b5-6168ccbbe097", |
| 96 | + "request_id": "c2b51ed6-da79-49ac-b411-206a42059509" |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +**Action Properties:** |
| 101 | +- `session_id`: Unique identifier for the current session |
| 102 | +- `request_id`: Unique identifier for the specific request |
| 103 | +- `type`: The type of action (e.g., "sign_transaction") |
| 104 | +- `source`: Origin of the action (e.g., "executor") |
| 105 | +- `data`: Transaction parameters including: |
| 106 | + - `chainId`: Network identifier (e.g., 11155111 for Sepolia) |
| 107 | + - `to`: Recipient's address |
| 108 | + - `data`: Transaction data (if any) |
| 109 | + - `value`: Amount to send in wei |
| 110 | + |
| 111 | +When handling actions: |
| 112 | +1. Parse the `message` field for human-readable transaction details |
| 113 | +2. Extract the transaction data from the `actions` array |
| 114 | +3. Present transaction details to the user for review |
| 115 | +4. Use a local wallet to sign the transaction |
| 116 | +5. Broadcast the signed transaction to the network |
| 117 | + |
| 118 | +**Example Implementation with thirdweb SDK:** |
| 119 | +```javascript |
| 120 | +import { |
| 121 | + createThirdwebClient, |
| 122 | + prepareTransaction, |
| 123 | + sendTransaction, |
| 124 | + privateKeyToAccount |
| 125 | +} from "thirdweb"; |
| 126 | + |
| 127 | +// Example function to handle the API response |
| 128 | +async function handleNebulaResponse(response) { |
| 129 | + // Initialize thirdweb client |
| 130 | + const client = createThirdwebClient({ |
| 131 | + secretKey: process.env.THIRDWEB_SECRET_KEY |
| 132 | + }); |
| 133 | + |
| 134 | + // Initialize account |
| 135 | + const account = privateKeyToAccount({ |
| 136 | + client, |
| 137 | + privateKey: process.env.EOA_PRIVATE_KEY |
| 138 | + }); |
| 139 | + |
| 140 | + // Check if we have any actions |
| 141 | + if (response.actions && response.actions.length > 0) { |
| 142 | + const action = response.actions[0]; |
| 143 | + |
| 144 | + // Parse the transaction data from the action |
| 145 | + const txData = JSON.parse(action.data); |
| 146 | + |
| 147 | + try { |
| 148 | + // Prepare transaction with client |
| 149 | + const transaction = prepareTransaction({ |
| 150 | + to: txData.to, |
| 151 | + data: txData.data, |
| 152 | + value: BigInt(txData.value), |
| 153 | + chain: txData.chainId, |
| 154 | + client |
| 155 | + }); |
| 156 | + |
| 157 | + // Send transaction with account |
| 158 | + const result = await sendTransaction({ |
| 159 | + transaction, |
| 160 | + account |
| 161 | + }); |
| 162 | + |
| 163 | + return result; |
| 164 | + } catch (error) { |
| 165 | + console.error("Error processing transaction:", error); |
| 166 | + throw error; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// Example usage |
| 172 | +const response = await fetch('https://nebula-api.thirdweb.com/chat', { |
| 173 | + method: 'POST', |
| 174 | + headers: { |
| 175 | + 'Content-Type': 'application/json', |
| 176 | + 'x-secret-key': 'YOUR_THIRDWEB_SECRET_KEY' |
| 177 | + }, |
| 178 | + body: JSON.stringify({ |
| 179 | + message: "send 0.0001 ETH on sepolia to vitalik.eth", |
| 180 | + execute_config: { |
| 181 | + mode: "client", |
| 182 | + signer_wallet_address: "0xc3F2b2a12Eba0f5989cD75B2964E31D56603a2cE" |
| 183 | + } |
| 184 | + }) |
| 185 | +}); |
| 186 | + |
| 187 | +const data = await response.json(); |
| 188 | +const result = await handleNebulaResponse(data); |
| 189 | +``` |
0 commit comments