> ## Documentation Index
> Fetch the complete documentation index at: https://docs.enterspeed.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Routes

The `routes` method is where you define how you fetch the generated view from the [Delivery API](/api-reference/delivery)

If your view should be routable you must implement the `routes` method and call context methods to build routes.

```js title="Routes example" theme={null}
routes: function(sourceEntity, context) {
    context.url(sourceEntity.url);
    context.handle('origin-' + sourceEntity.originId);
}
```

# RoutesContext object

The `RoutesContext` object is passed into the `routes` method and gives you access to a set of methods which is described below.

## Methods

| Method            | Description                                                                                                                 |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------- |
| [handle](#handle) | A handle is a key from which you can fetch the view. A view can have multiple handles.                                      |
| [lookup](#lookup) | Lookup allows you to search source entities using a filter string and work with the source entities directly in the schema. |
| [url](#url)       | A string value that represents the url you want to fetch the view by. A view can only have one url.                         |

### handle

Handle allows you to specify a key from which you can fetch the view from the Delivery API.

`handle(handle)`

#### Parameters

| Parameter | Type   | Description                                             |
| --------- | ------ | ------------------------------------------------------- |
| `handle`  | string | The key you use to fetch the view from the Delivery API |

### lookup

Lookup allows you to search source entities using a filter string and build dynamic handles or URLS based on data from other source entities.

Note the `lookup` is an async function so you have to use [async/await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) or [`then`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) when you are using the `lookup` function.

`lookup(filter)`

<Note>
  The `lookup` function is limited to a maximum number of items to return, based on the tenant plan. It can however be increased per tenant basis. Please reach out to Enterspeed if needed.

  The default limits are:

  * Free & Premium plans: 100 items
  * Enterprise plan: 500 items
</Note>

#### Parameters

| Parameter       | Type   | Description                                                                                                                                                                                                                                                                                                                                                                                            |
| --------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `filter`        | string | A filtering criteria. [See list of filter examples](/enterspeed/reference/filter-expressions)                                                                                                                                                                                                                                                                                                          |
| `lookupOptions` | object | An optional object with the following structure: <br />`{ excludeProperties?: boolean; }`<br /><br />If `excludeProperties` is set to `true` the `lookup` will only return the base fields of the source entities and not all custom properties. This will help improve performance if you don't need the custom properties, but only some of the base fields like `originId`, `originParentId`, `url` |

#### Required function calls

After the `lookup` function it's required to call `toPromise` to excecute the query and return a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).

<Accordion title="toPromise">
  Using the toPromise function excecutes the query and return a promise you must resolve.

  ```js title="look toPromise" theme={null}
  const category: await context
                  .lookup(`originId eq '${sourceEntity.properties.categoryId}'`)
                  .toPromise()
  ```
</Accordion>

#### Optional function calls

To filter the source entities even further you can call some of the following optional functions.

<AccordionGroup>
  <Accordion title="limit">
    The `limit` function limits the number of source entities.

    #### Parameters

    | Parameter | Type   | Description                                      |
    | --------- | ------ | ------------------------------------------------ |
    | `limit`   | number | The maximum number of source entities to return. |

    ```js title="look and limit" theme={null}
    const mainCategory: await context
                    .lookup(`type eq 'mainCategory'`)
                    .limit(1)
                    .toPromise()
    ```
  </Accordion>

  <Accordion title="orderBy">
    The order sequence of the source entities.

    #### Parameters

    | Parameter | Type                                                  | Description                                       |
    | --------- | ----------------------------------------------------- | ------------------------------------------------- |
    | `orderBy` | \{ propertyName: string, direction: "asc" \| "desc" } | Allows you to specify your desired sorting order. |

    ```js title="lookup and orderBy" theme={null}
    const categories: await context
                    .lookup(`type eq 'category'`)
                    .orderBy({ propertyName: "properties.createdDate", direction: "desc"})
                    .toPromise()
    ```
  </Accordion>

  <Accordion title="sourceGroup">
    The sourceGroup function lets you specify the source group. By default the source group of the current source entity is used.

    #### Parameters

    | Parameter     | Type   | Description                                                                                                                                                       |
    | ------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
    | `sourceGroup` | string | Allows you to define a different source group. The sourceGroupAlias should be equal to the desired source group alias where you want to look for source entities. |

    If not defined, it uses the current source group.

    ```js title="lookup and sourceGroup" theme={null}
    const categories: await context
                    .lookup(`type eq 'category'`)
                    .sourceGroup("anotherSourceGroup")
                    .toPromise()
    ```
  </Accordion>
</AccordionGroup>

#### Examples

```js title="lookup using async/await" theme={null}
routes: async function(sourceEntity, context) {
    const categories = await context.lookup(`originId in (${sourceEntity.properties.categoryIds.map(c => `'${c}'`)})`).toPromise();

    categories.forEach((category) => {
        context.url(`${category.url}/${sourceEntity.properties.slug}`)
    })
}
```

```js title="lookup using 'then'" theme={null}
routes: function(sourceEntity, context) {
    context.lookup(`originId in (${sourceEntity.properties.categoryIds.map(c => `'${c}'`)})`)
            .toPromise()
            .then((categories) => {
                categories.forEach((category) => {
                    context.url(`${category.url}/${sourceEntity.properties.slug}`)
                })
            });
}
```

### url

url allows you to specify a url from which you can fetch the view from the delivery.

`url(url)`

#### Parameters

| Parameter | Type   | Description                                             |
| --------- | ------ | ------------------------------------------------------- |
| `url`     | string | The url you use to fetch the view from the Delivery API |

##### Optional function calls

To the `url` function you can call some of the following optional functions.

<Accordion title="redirects">
  The `redirects` function creates incomming redirects to a specific url.

  <Info>
    Setting the redirects to an empty array clears any potential implicit redirects.
  </Info>

  #### Parameters

  | Parameter   | Type      | Description                                    |
  | ----------- | --------- | ---------------------------------------------- |
  | `redirects` | string\[] | Adds a list of incomming redirects to the URL. |

  ```js title="routes with url and redirects" theme={null}
  context
      .url(sourceEntity.url)
      .redirects(sourceEntity.redirects);
  ```
</Accordion>

## Examples

```js title="routes example with url and multiple handles" theme={null}
routes: function(sourceEntity, context) {
    context.url(sourceEntity.url);
    context.handle('origin-' + sourceEntity.originId);
    context.handle(sourceEntity.properties.entityKey);
}
```

```js title="routes example with single url" theme={null}
routes: function(sourceEntity, context) {
    context.url(sourceEntity.url);
}
```

```js title="routes can also be expressed as an arrow function expression to make it even more compact" theme={null}
routes: (sourceEntity, context) => context.url(sourceEntity.url)
```

You can then fetch the view using our [Delivery API](/api-reference/delivery) using either the url or one of the handles.

You can also fetch multiple views in one request, although in this case it doesn't make sense to fetch the same view three time, but just to demonstrate if you want to fetch multiple different views in one request.

```
https://delivery.enterspeed.com/v2
                        ?url=/fairy-tales/the-emperors-new-clothes/
                        &handle=origin-1234
                        &handle=5678
```
