---
title: "How to get URL parameters in LWC?"
url: "https://blog.beyondthecloud.dev/blog/get-url-parameters-in-lwc"
author: "Piotr Gajek"
published: "2022-07-15"
updated: "2025-02-27"
tags: "javascript, lwc, salesforce, url"
---

# How to get URL parameters in LWC?

By [Piotr Gajek](https://blog.beyondthecloud.dev/author/piotr-gajek.md), July 15, 2022

Hello Folks,  
Today I wanna show you how to get URL parameters in the **LWC** component using **CurrentPageReference**.

Let’s get started.

## Introduction

The easiest way to get url params will be to use **CurrentPageReference**.

* _currentPageReference.state_ – contains the key – value pairs of URL query parameters.
* _currentPageReference.state.parameterName_ – allows to get specific param from URL.

![lwc-url-params](https://wordpress.beyondthecloud.dev/wp-content/uploads/2022/07/lwc-url-params.png)

```js
// currentPageReference
{
   attributes: {
      name: URL_Test_Page__c
   },
   state: {
      lang: en_US,
      type: test-type,
      id: 000000000001
   },
   type: comm__namedPage
}
```

As you can see in the code above – **currentPageReference** includes page API Name and [type](https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference%5Fpage%5Freference%5Ftype).

Reference page types that are supported:

* App – **standard\_\_app**
* Lightning Component – **standard\_\_component**
* Knowledge Article – **standard\_\_knowledgeArticlePage**
* Login Page – **comm\_\_loginPage**
* Named Page (Communities) – **comm\_\_namedPage**
* Named Page (Standard) – **standard\_\_namedPage**
* Navigation Item Page – **standard\_\_navItemPage**
* Object Page – **standard\_\_objectPage**
* Record Page – **standard\_\_recordPage**
* Record Relationship Page – **standard\_\_recordRelationshipPage**
* Web Page – **standard\_\_webPage**

**Assumptions**

* Since the key-value pairs of `PageReference.state` are serialized to URL query parameters, all the values must be strings.
* **@wire service getStateParameters will be fire automatically every time URL params will change.**

## Code

```js
import { LightningElement, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';

export default class MyComponentName extends LightningElement {

    urlId = null;
    urlLanguage = null;
    urlType = null;

    @wire(CurrentPageReference)
    getStateParameters(currentPageReference) {
       if (currentPageReference) {
          this.urlId = currentPageReference.state?.id;
          this.urlLanguage = currentPageReference.state?.lang;
          this.urlType = currentPageReference.state?.type;
       }
    }
}
```

## Repository

[Github](https://github.com/beyond-the-cloud-dev/posts/blob/main/retrieve-url-params-in-lwc/lwc/lwcParams/lwcParams.js)

---

If you have some questions feel free to ask in the comment section below. 🙂

Was it helpful? Check out our other great posts [here](https://beyondthecloud.dev/blog).

---

## Resources

* [Navigation – CurrentPageReference](https://developer.salesforce.com/docs/component-library/bundle/lightning-navigation/documentation)
* [Add Query Parameters](https://developer.salesforce.com/docs/component-library/documentation/en/lwc/use%5Fnavigate%5Fadd%5Fparams%5Furl)
