# Fetching Data

Fetching data from the internet is relatively simple to do in Vortex. The standard library includes a module called `requests` that allows you to perform HTTP(S) actions. We'll use it to fetch some fake data from an API, parse it and write it to a file.

{% code title="fetch.vtx" overflow="wrap" %}

```rust
import io
import requests
import [parse, serialize] : json
import [join] : string
import [] : functional

const headers = {
    "Content-Type": "application/json"
}

const random_users = (count = 100) => {
    const users = requests.get(f"https://randomuser.me/api/?nat=us,au,gb,ca&results=${count}", headers).body.parse()
    return users.results
}

random_users()
.filter((user) => user.registered.age >= 18)
.sort((a, b) => a.registered.age < b.registered.age)
.map((user, index) => f"${index+1}: ${user.name.title} ${user.name.first} ${user.name.last}, ${user.registered.age} (${user.email})")
.chain((users) => users.insert(f"People (${users.length()})\n------------\n", 0))
.join("\n")
.chain((users) => io.writef("people.txt", users))

io.readf("people.txt").println()
```

{% endcode %}

First we import all the modules/functions we need.

We define our headers that we'll be sending alongside our request. We then define the function that will retrieve our users from the `randomuser` API.

This function uses `requests.get()` to retrieve the data we need, sending the headers we defined earlier with it.

We then use json to parse the body of our response, and return it.

It's important to note that calling the random\_users function will block the main thread. The requests module is not designed to be non-blocking. If you wanted it to be non-blocking, you will need to use a Future (`import [Future] : future`) to call the function in another thread.

Once we have our parsed data, we chain a few different functions to it to modify the output. The functions `chain`, `filter` and `map` are imported from the `functional` module.

We then join our strings together using the `join` function imported from `string`.

The last chain call writes the output to a file named `people.txt` using `io.writef`.

Lastly, we read the file we just created using `io.readf` and print the results to the console.

This is an example output:

{% code title="stack.vtx" overflow="wrap" %}

```rust
People (23)
------------

1: Mr Curtis Macrae, 18 (curtis.macrae@example.com)
2: Mr Kent Frazier, 18 (kent.frazier@example.com)
3: Ms Ariane Grewal, 18 (ariane.grewal@example.com)
4: Miss Justine Bouchard, 18 (justine.bouchard@example.com)
5: Miss Alexis Wilson, 19 (alexis.wilson@example.com)
6: Mr Jordan Howell, 19 (jordan.howell@example.com)
7: Mr Fernando Rodriguez, 19 (fernando.rodriguez@example.com)
8: Mrs Emma Chu, 19 (emma.chu@example.com)
9: Ms Noémie Wilson, 19 (noemie.wilson@example.com)
10: Miss Maya Stewart, 19 (maya.stewart@example.com)
11: Mrs Erin Graham, 19 (erin.graham@example.com)
12: Mr Louis Clark, 20 (louis.clark@example.com)
13: Miss Katherine Weaver, 20 (katherine.weaver@example.com)
14: Mr Seth Morris, 20 (seth.morris@example.com)
15: Ms Sylvia Bates, 20 (sylvia.bates@example.com)
16: Miss Eliza Ford, 20 (eliza.ford@example.com)
17: Mr Cameron Mccoy, 20 (cameron.mccoy@example.com)
18: Mr Zachary Williams, 21 (zachary.williams@example.com)
19: Ms Rebecca Stephens, 21 (rebecca.stephens@example.com)
20: Mr Thomas Curtis, 21 (thomas.curtis@example.com)
21: Ms Lily Miller, 21 (lily.miller@example.com)
22: Mrs Zoe Taylor, 21 (zoe.taylor@example.com)
23: Mrs Amber Carter, 21 (amber.carter@example.com)
```

{% endcode %}

Apart from fetching data, you can use requests to post, patch and delete as well. Visit the [standard modules reference guide](/vortex-docs/standard-modules/requests.md) for more information.


---

# 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://dibs.gitbook.io/vortex-docs/examples/fetching-data.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.
