# generateUrl()

Generates a URL for a specified page reference.

## Properties

| Method Input    | Type   | Required/Optional | Description                                                                                                                                                                                                                                  |
| --------------- | ------ | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `context`       | String | Required          | The ID of the parent router container nearest to the component that's generating a URL. A `ContextId` is an opaque value that you get from the [`NavigationContext`](/docs/platform/lwr/references/csr-reference/navigationcontext.md) wire. |
| `pageReference` | String | Required          | The [`PageReference`](/docs/platform/lwr/references/csr-reference/pagereference.md) value for which to generate a URL.                                                                                                                       |

## Syntax

```javascript
import { LightningElement, api, wire } from 'lwc';
import { NavigationContext, generateUrl } from 'lwr/navigation';
import type { ContextId, PageReference } from 'lwr/navigation';

generateUrl(
    context: ContextId,
    pageReference: PageReference
    ): string | null
```

## Returns

- If `pageReference` is valid, `generateUrl()` returns a string with the generated URL for the specified route.
- If `pageReference` is invalid, it returns `null`.
- If `context` is invalid, `generateUrl()` fails.

**See Also**

- [Simple Client-Side Routing: Navigate](/docs/platform/lwr/guide/lwr-routing-example-simple.html#navigate)
