Sana Assistant (online)
Table of Contents

UseApi hook

The useApi hook provides information about application Api available for addons. The hook signature is described below:

export function useApi(): Api;

This hook has no parameters and returns object with available application Api.

Example:

const query = `
query {
  languages {
    displayName
  }
}`;

const ApiBlock = () => {
  const api = useApi();
  const [fetchResponse, setFetchResponse] = useState<{ databaseOnline: boolean }>();
  const [graphResponse, setGraphResponse] = useState<{ displayName: string }[]>();

  useEffect(() => {
    api.fetch('/system/checks').subscribe(setFetchResponse);
    api.graphApi(query).subscribe(res => setGraphResponse(res.languages));
  }, []);

  // ...
};

See also