# REST

## Features of this Example

* Basic authentication
* Storing and Reading Environment Variables in Asserted
* Test the list, create, get, update, and remove REST endpoints

### Walkthrough

This example was referenced in a walkthrough about [Node API Health Checks and Uptime](https://asserted.io/posts/node-api-health-check-uptime)

### Try it out

Repo is available [here](https://github.com/assertedio/node-uptime).

```bash
# Clone example
git clone https://github.com/assertedio/node-uptime

# Enter directory and install
cd graphql-uptime/
npm install

# Run asserted tests
npm run test:asrtd
```

### Tests

Can also be viewed on github here.

```javascript
const { expect } = require('chai');
const got = require('got');
const getenv = require('getenv');
const path = require('path');

require('dotenv').config({ path: path.join(__dirname, './.env') });

const TOKEN = getenv('TOKEN');

const client = got.extend({
  prefixUrl: 'http://localhost:3000',
  headers: { authorization: TOKEN },
});

describe('node api tests', () => {
  let userId;

  it('get all users', async () => {
    const { data } = await client.get('users').json();
    expect(data.length).to.eql(4);
  });

  it('create user', async () => {
    const { data } = await client.post('users', { json: { name: 'Foo Bario', email: 'foo@bar.io' }}).json();

    const { id, name, email } = data;
    userId = id;

    expect(userId).to.exist;
    expect(name).to.eql('Foo Bario');
    expect(email).to.eql('foo@bar.io');
  });

  it('get user', async () => {
    expect(userId).to.exist;

    const { data } = await client.get('users/' + userId).json();

    const { id, name, email } = data;

    expect(id).to.eql(userId);
    expect(name).to.eql('Foo Bario');
    expect(email).to.eql('foo@bar.io');
  });

  it('update user', async () => {
    expect(userId).to.exist;

    const { data } = await client.put('users/' + userId, { json: { name: 'Bar Yaz', email: 'bar@yaz.io' }}).json();

    const { id, name, email } = data;

    expect(id).to.eql(userId);
    expect(name).to.eql('Bar Yaz');
    expect(email).to.eql('bar@yaz.io');
  });

  it('remove user', async () => {
    expect(userId).to.exist;

    await client.delete('users/' + userId).json();

    const { data } = await client.get('users/' + userId).json();
    expect(data).to.eql(null);
  });
});

```


---

# 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.asserted.io/examples/rest-api.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.
