# useAPI

The `useAPI` hook is designed to make API calls and handle errors gracefully. It returns the loading state and the fetched data.

```javascript
const { data, loading } = useAPI(url, method, trigger);
```

The `useAPI` hook returns an object with two properties: `loading` and `data`. You can use the loading value to handle the loading state in the parent object. Data will be returned when the API response is returned.

The hook will automatically forward any errors to the error handler in the `ViewContext`.

### Parameters&#x20;

| param   | description                      | value                   |
| ------- | -------------------------------- | ----------------------- |
| url     | API endpoint URL to call         | url string              |
| method  | HTTP method                      | string **default: GET** |
| trigger | Boolean to re-start the API call | optional                |

### Example

```javascript
import { useState } from 'react';
import { useAPI, Loader, Button } from 'components/lib';

const MyComponent = () => {

  const [trigger, setTrigger] = useState(false);
  const { data, loading } = useAPI('/user', 'GET', trigger);

  return (
    <div>
    
      { loading ? 
        <Loader/> : 
        <p>Data: { JSON.stringify(data) }</p> }
      
      <Button action={() => setTrigger(!trigger)}>
        Refresh Data
      </Button>
      
    </div>
  );
};
```


---

# Agent Instructions: 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:

```
GET https://docs.usegravity.app/gravity-web/hooks/useapi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
