# FAQ #2 - How to get a list of events?

**URL:** <https://forum.flow.com/t/faq-2-how-to-get-a-list-of-events/539>\
**Category:** Flow JavaScript SDK\
**Tags:** flow-sdk-js\
**Created:** [August 7, 2020, 11:45am UTC](https://forum.flow.com/t/faq-2-how-to-get-a-list-of-events/539 "2020-08-07T11:45:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxStarka](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/maxstarka/32/56_2.png) [@MaxStarka](https://forum.flow.com/u/MaxStarka)\
**Post date:** [August 7, 2020, 11:45am UTC](https://forum.flow.com/t/faq-2-how-to-get-a-list-of-events/539/1 "2020-08-07T11:45:02Z")

</div>

You can query Flow for a list of events in a specific range by creating following helper method:

```auto
const getEvents = async (params) => {
  // Define event type from params
  const { contractAddress, contractName, eventName } = params;
  const eventType = `A.${contractAddress}.${contractName}.${eventName}`;

  const { from = 0, to } = params;
  let toBlock;
  if (to === undefined) {
    // Get latest block
    const blockResponse = await fcl.send(
      await sdk.build([sdk.getLatestBlock()])
    );
    toBlock = blockResponse.latestBlock.height;
  } else {
    toBlock = to;
  }

  const response = await fcl.send(
    await sdk.build([sdk.getEvents(eventType, from, toBlock)])
  );

  // Return a list of events
  return response.events;
};

```

And then use it, for example, like this:

```auto
const getHelloEvents = async () => {
  const events = await getEvents({
    contractName: "HelloWorld",
    contractAddress: "01cf0e2f2f715450", // note the address is without "0x" prefix
    eventName: "CustomEvent",
  });
  console.log({ events });
};

```

I’ve also updated the repository, which this method and others to help you deploy contract, send transactions and execute scripts on Flow. You can reference it here:

> **[GitHub - MaxStalker/flow-stream-code-along](https://github.com/MaxStalker/flow-stream-code-along)**
>
> Contribute to MaxStalker/flow-stream-code-along development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![jayesh\_wayesh](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/jayesh_wayesh/32/121_2.png) [@jayesh\_wayesh](https://forum.flow.com/u/jayesh_wayesh)\
**Post date:** [August 7, 2020, 11:03pm UTC](https://forum.flow.com/t/faq-2-how-to-get-a-list-of-events/539/2 "2020-08-07T23:03:08Z")

</div>

Hey @MaxStarka ! 👋  
I tried creating a custom event on my own.

![event_error1.png](https://us1.discourse-cdn.com/flex022/uploads/flow/original/1X/26d0670044c0d6221c638234a5417526bb3171b0.png)

However, I’m facing this problem as can be seen below -  
value[0].name should be “id” not “ethid”  
value[1].name should be “ethid” not “id”

![event_error2](https://us1.discourse-cdn.com/flex022/uploads/flow/original/1X/fd57ab40cf35bce9cf40a97e0cb50d3190c5829f.png)

Any guesses what’s wrong with my code?

---

<div class="post-metadata">

**Author:** ![MaxStarka](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/maxstarka/32/56_2.png) [@MaxStarka](https://forum.flow.com/u/MaxStarka)\
**Post date:** [August 8, 2020, 9:35am UTC](https://forum.flow.com/t/faq-2-how-to-get-a-list-of-events/539/3 "2020-08-08T09:35:46Z")

</div>

All cool with your code, @jayesh_wayesh. It’s a known error that arguments are alphabetically sorted. The team is working on it 😬

As a temporary solution, in your Cadence code for event declaration, add prefixes `a_`, `b_`, etc. like this (don’t forget to update the signature, when you emit them 😉)

```auto
pub event Converted(a_id: UInt64, b_ethid: String)

```

Create a helper method:

```auto
export const fixNames = (item) => {
  var result = {}

  for (let name of Object.keys(item)) {
    const value = item[name]
    const fixedName = name.replace(/\w+_/,'')
    result[fixedName] = value
  }

  return result
}

```

And your method for requesting events :

```auto
const getEvents = async (params) => {
  // Define event type from params
  const { contractAddress, contractName, eventName } = params;
  const eventType = `A.${contractAddress}.${contractName}.${eventName}`;

  const { from = 0, to } = params;
  let toBlock;
  if (to === undefined) {
    // Get latest block
    const blockResponse = await fcl.send(
      await sdk.build([sdk.getLatestBlock()])
    );
    toBlock = blockResponse.latestBlock.height;
  } else {
    toBlock = to;
  }

  const response = await fcl.send(
    await sdk.build([sdk.getEvents(eventType, from, toBlock)])
  );

  // Decode server response, so we could get properly formatted values
  const events = await fcl.decode(response);

  // Now let's purge prefixes from names
  const fixedEvents = events.map((item) => {
    const { data } = item;
    item.data = fixNames(data);
    return item;
  });

  // Return a list of events
  return fixedEvents;
};

```

This way you will be able to utilize `fcl.decode` method and later you can redeploy the code without prefixes and it should (theoretically) still work without updating your Javascript code 😉

---

<div class="post-metadata">

**Author:** ![jayesh\_wayesh](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/jayesh_wayesh/32/121_2.png) [@jayesh\_wayesh](https://forum.flow.com/u/jayesh_wayesh)\
**Post date:** [August 9, 2020, 9:59am UTC](https://forum.flow.com/t/faq-2-how-to-get-a-list-of-events/539/4 "2020-08-09T09:59:45Z")

</div>

Thanks @MaxStarka 🙌

---

<div class="post-metadata">

**Author:** ![flowuser](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@flowuser](https://forum.flow.com/u/flowuser)\
**Post date:** [March 12, 2021, 1:07pm UTC](https://forum.flow.com/t/faq-2-how-to-get-a-list-of-events/539/5 "2021-03-12T13:07:48Z")

</div>

Hello @MaxStarka,  
I have just started using flow blockchain and trying to listen to event.  
I have tried the code that you have mentioned and it gives me error “err Error: failed to get events: could not look up block: key not found”

can you please point what am i doing wrong? i tried with “0x” prefix and also without that

> const events = await getEvents({
> 
> ```
> contractName: "TopShot",
> 
> contractAddress: "0b2a3299cc857e29", // note the address is without "0x" prefix
> 
> eventName: "PlayAddedToSet",
> 
> });
> 
> ```
