# Querying Elixir API from a UI

**URL:** <https://forum.elixirforum.com/t/querying-elixir-api-from-a-ui/23335>\
**Category:** Questions\
**Tags:** phoenix, beginner\
**Created:** [June 22, 2019, 11:11pm UTC](https://forum.elixirforum.com/t/querying-elixir-api-from-a-ui/23335 "2019-06-22T23:11:57Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![amnu3387](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@amnu3387](https://forum.elixirforum.com/u/amnu3387)\
**Post date:** [June 23, 2019, 9:47am UTC](https://forum.elixirforum.com/t/querying-elixir-api-from-a-ui/23335/5 "2019-06-23T09:47:10Z")

</div>

I think if you’re using the browser fetchAPI you need to create the headers from the constructor, I use this:

```elixir
export function requester({ method, url, data, accept, content_type }, token) {
    let body = typeof data === "string" ? data : JSON.stringify(data);

    token = token ? token : "";
    content_type = content_type ? content_type : "application/json";
    accept = accept ? accept : "application/json";
    
    if (process && process.server) {
        const fetch = require("node-fetch");
        let headers = {
            "Bearer": token,
            "Content-Type": content_type,
            "Accept": accept
        };
        let request = {
            method: method,
            headers: headers,
            body: body,
            mode: "cors"
        };

        return fetch(url, request)
            .then(response => response.json())
            .catch(error => { return {error: error.message} });
        
    } else {
        
        let headers = new Headers({
            "Bearer": token,
            "Content-Type": content_type,
            "Accept": accept
        });
        
        let request = new Request(url, {
            method: method,
            headers: headers,
            body: body,
            mode: "cors"
        });
        
        return fetch(request)
            .then(response => response.json())
            .catch(error => { return {error: error.message} });
    }
}

```

---

_[View the full topic](https://forum.elixirforum.com/t/querying-elixir-api-from-a-ui/23335)._
