> For the complete documentation index, see [llms.txt](https://notes.danfitz.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.danfitz.com/javascript/completenodejs/7-accessing-api-from-browser.md).

# Accessing API From Browser

The goal of this section is to learn how to create an **API endpoint** where the data provided is *customizable based on user input*, i.e., query parameters.

## The Query String

For the user to get more custom data, we can accept query strings appended to our API endpoint. This appears in the `req.query` object returned by express in the route handler callback.

```js
app.get('/api', (req, res) => {
    console.log(req.query)
})

// If you go to localhost:3000/api?type=games...
// req.query returns { type: 'games' }
```

If you want to make a query string **required**, you just need to add conditional logic that sends an error response if the query string isn't provided:

```js
if (!req.query.type) {
    res.send({
        error: 'You must provide a type query'
    })
} else {
    res.send(data)
}
```

**Note**: Never have 2 `res.send` calls execute. Every HTTP request can only have one response back. Express will throw an error otherwise.
