> For the complete documentation index, see [llms.txt](https://cll-devrel.gitbook.io/bootcamp-2024/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cll-devrel.gitbook.io/bootcamp-2024/9.-off-chain-data-with-chainlink-functions/remix.md).

# Remix

{% embed url="<https://remix.ethereum.org/>" fullWidth="false" %}

### Connect MetaMask to Remix

In Remix, Go to the "DEPLOY & RUN TRANSACTIONS" icon on left side:

<div align="left"><figure><img src="/files/mgAFMbG7ZTbvC71nFBLW" alt=""><figcaption><p>DEPLOY &#x26; RUN TRANSACTIONS ICON</p></figcaption></figure></div>

In the “ENVIRONMENT” drop-down menu, select “Injected Provider - MetaMask”:

<div align="left"><figure><img src="/files/10HoA2a3tXciFtj3hwol" alt=""><figcaption><p>Select Injected Provider - MetaMask</p></figcaption></figure></div>

Make sure you are on Custom (43113) network, which is Avalanche Fuji Testnet:

<div align="left"><figure><img src="/files/bYnxHMfQwnmaqq42Tmrz" alt=""><figcaption><p>Connected to (43113) - Avalanche Fuji Testnet</p></figcaption></figure></div>

***

### Creating your Smart Contract

Create a smart contract called `GettingStartedFunctionsConsumer.sol`. First, go to the 1. “FILE EXPLORER” icon and then click the 2. “Create new file” icon:

<div align="left"><figure><img src="/files/s56tHUP9hrPshvWcGvwQ" alt=""><figcaption><p>Create a new Solidity file</p></figcaption></figure></div>

Name the new Solidity file `GettingStartedFunctionsConsumer.sol` and a new tab will open in Remix:

<figure><img src="/files/WjQ8RojYH3AS48xdUyXn" alt=""><figcaption><p>Blank Solidity file</p></figcaption></figure>

***

### Adding Code&#x20;

Go to [Getting Started | Chainlink Documentation](https://docs.chain.link/chainlink-functions/getting-started/#deploy-a-functions-consumer-contract-on-sepolia/) and you will see a "Open in Remix" button, click that and Remix will open with the code loaded.

<figure><img src="/files/P59obWr96cIuDUdgAMAG" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ITl23N3WXm3TzPGYi6iB" alt=""><figcaption><p>GettingStartedFunctionsConsumer.sol</p></figcaption></figure>

Since contract we copied over  was written for deployment on Sepolia, you will need to change configuration parameters like the `address router` and `donID` to the relevant addresses for Avalanche Fuji Testnet.  \
\
Specifically:

<figure><img src="/files/h40SYVhLQbzbPHtFm41v" alt=""><figcaption></figcaption></figure>

Update the `address router` and `donID` by copying and pasting the following addresses into `GettingStartedFunctionsConsumer.sol`:

{% code title="address router" %}

```solidity
0xA9d587a00A31A52Ed70D6026794a8FC5E2F5dCb0
```

{% endcode %}

{% code title="donID" %}

```solidity
0x66756e2d6176616c616e6368652d66756a692d31000000000000000000000000
```

{% endcode %}

You can get these configuration details from [Supported Networks | Chainlink Documentation](https://docs.chain.link/chainlink-functions/supported-networks#avalanche-fuji-testnet/). Always cross-check that for the correct configs!

3. Copy the full code below and paste into `GettingStartedFunctionsConsumer.sol`:

{% code title="GettingStartedFunctionsConsumer.sol" %}

````solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

// Deploy on Fuji

import {FunctionsClient} from "@chainlink/contracts@1.1.0/src/v0.8/functions/v1_0_0/FunctionsClient.sol";
import {ConfirmedOwner} from "@chainlink/contracts@1.1.0/src/v0.8/shared/access/ConfirmedOwner.sol";
import {FunctionsRequest} from "@chainlink/contracts@1.1.0/src/v0.8/functions/v1_0_0/libraries/FunctionsRequest.sol";

/**
 * Request testnet LINK and ETH here: https://faucets.chain.link/
 * Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/resources/link-token-contracts/
 */

/**
 * @title GettingStartedFunctionsConsumer
 * @notice This is an example contract to show how to make HTTP requests using Chainlink
 * @dev This contract uses hardcoded values and should not be used in production.
 */
contract GettingStartedFunctionsConsumer is FunctionsClient, ConfirmedOwner {
    using FunctionsRequest for FunctionsRequest.Request;

    // State variables to store the last request ID, response, and error
    bytes32 public s_lastRequestId;
    bytes public s_lastResponse;
    bytes public s_lastError;

    // Custom error type
    error UnexpectedRequestID(bytes32 requestId);

    // Event to log responses
    event Response(
        bytes32 indexed requestId,
        string character,
        bytes response,
        bytes err
    );

    // Router address - Hardcoded for Fuji
    // Check to get the router address for your supported network https://docs.chain.link/chainlink-functions/supported-networks
    address router = 0xA9d587a00A31A52Ed70D6026794a8FC5E2F5dCb0;

    // JavaScript source code
    // Fetch character name from the Star Wars API.
    // Documentation: https://swapi.info/people
    string source =
        "const characterId = args[0];"
        "const apiResponse = await Functions.makeHttpRequest({"
        "url: `https://swapi.info/api/people/${characterId}/`"
        "});"
        "if (apiResponse.error) {"
        "throw Error('Request failed');"
        "}"
        "const { data } = apiResponse;"
        "return Functions.encodeString(data.name);";

    //Callback gas limit
    uint32 gasLimit = 300000;

    // donID - Hardcoded for Fuji
    // Check to get the donID for your supported network https://docs.chain.link/chainlink-functions/supported-networks
    bytes32 donID =
        0x66756e2d6176616c616e6368652d66756a692d31000000000000000000000000;

    // State variable to store the returned character information
    string public character;

    /**
     * @notice Initializes the contract with the Chainlink router address and sets the contract owner
     */
    constructor() FunctionsClient(router) ConfirmedOwner(msg.sender) {}

    /**
     * @notice Sends an HTTP request for character information
     * @param subscriptionId The ID for the Chainlink subscription
     * @param args The arguments to pass to the HTTP request
     * @return requestId The ID of the request
     */
    function sendRequest(
        uint64 subscriptionId,
        string[] calldata args
    ) external onlyOwner returns (bytes32 requestId) {
        FunctionsRequest.Request memory req;
        req.initializeRequestForInlineJavaScript(source); // Initialize the request with JS code
        if (args.length > 0) req.setArgs(args); // Set the arguments for the request

        // Send the request and store the request ID
        s_lastRequestId = _sendRequest(
            req.encodeCBOR(),
            subscriptionId,
            gasLimit,
            donID
        );

        return s_lastRequestId;
    }

    /**
     * @notice Callback function for fulfilling a request
     * @param requestId The ID of the request to fulfill
     * @param response The HTTP response data
     * @param err Any errors from the Functions request
     */
    function fulfillRequest(
        bytes32 requestId,
        bytes memory response,
        bytes memory err
    ) internal override {
        if (s_lastRequestId != requestId) {
            revert UnexpectedRequestID(requestId); // Check if request IDs match
        }
        // Update the contract's state variables with the response and any errors
        s_lastResponse = response;
        character = string(response);
        s_lastError = err;

        // Emit an event to log the response
        emit Response(requestId, character, s_lastResponse, s_lastError);
    }
}
```
````

{% endcode %}

***

### Compile your Contract

You will need to compile your contract, so navigate to the 1. “SOLIDITY COMPILER” icon and then 2. click on the “Compile” button. You will see a green check when it has compiled successfully on the icon.

<figure><img src="/files/16zm7stQEDAPjaVBg9BH" alt=""><figcaption><p>Successful compiled contract</p></figcaption></figure>

***

### Deploy your Smart Contract

Time to deploy your contract, perform the following steps:

1. Go to the "DEPLOY & RUN TRANSACTIONS" tab, and make sure you're connected to Fuji
2. Click the "Deploy" button at the bottom
3. MetaMask will pop-up and click the "Confirm" button

<figure><img src="/files/qNTp10TsYintDl4VdaCk" alt=""><figcaption><p>Deployment</p></figcaption></figure>

Upon a successful deployment, you will see your contract at the bottom the of "DEPLOY & RUN TRANSACTIONS" section:

<div align="left"><figure><img src="/files/EUzYHozG3kKlvDNLCCSs" alt=""><figcaption><p>Successfully deployed your contract</p></figcaption></figure></div>

In the next section, we will create a Chainlink Functions Subscription.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cll-devrel.gitbook.io/bootcamp-2024/9.-off-chain-data-with-chainlink-functions/remix.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
