# Reactium + REST

### Creating a simple API

Use axios, native fetch, or a library of your choosing, in some API domain folder of your choice, generate a new `reactium-hooks.js` file.

{% code title="reactium-hooks.js (version 1)" %}

```javascript
import Reactium from 'reactium-core/sdk';
import axios from 'axios';

const helloAPI = {
  hello() {
    return axios.get('http://demo7132150.mockable.io/hello');
  }
};

// TODO: register API here
```

{% endcode %}

### Register your API

The above is incomplete. In order to make this API available to the rest of your app, let's register it.

{% code title="reactium-hooks.js (version 2)" %}

```javascript
import Reactium from 'reactium-core/sdk';
import axios from 'axios';

const helloAPI = {
  contructor(baseURL) {
    this.axios = axios.create({
      baseURL,
    });
  }
  hello() {
    return this.axios.get('/hello');
  }
};

Reactium.API.register({
  name: 'helloAPI',
  api: new helloAPI('http://demo7132150.mockable.io'),
});
```

{% endcode %}

Now that we've registered our REST API with the Reactium SDK, we can use it throughout out app as `Reactium.helloAPI`. Congrats! You just extended the Reactium SDK with your own API!

### Use your API

Let's say we want to surface our `/hello` API endpoint in a React component. We can now access our `helloAPI` on the Reactium SDK directly.

{% code title="src/app/components/Hello/MyHelloComponent.js" %}

```javascript
import React, { useState } from 'react';
import Reactium, { useAsyncEffect } from 'reactium-core/sdk';

const MyHelloComponent = ({ startingMsg = ''}) => {
  const [ message, setMessage ] = useState(startingMsg);
  
  useAsyncEffect(async mounted => {
    let newMessage;
    try {
      const { data } = await Reactium.helloAPI.hello();
      newMessage = data.msg;
    } catch (error) {
      newMessage = 'Oh no!';
    }
    
    if (mounted()) {
      setMessage(newMessage);
    }
  });
  
  return (<div>{message}</div>);
};

MyHelloComponent.defaultProps = {
  startingMsg: 'Waiting...',
};

export default MyHelloComponent;
```

{% endcode %}


---

# 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.reactium.io/reactium/reactium-guides/reactium-+-rest.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.
