# How do I retrieve the principal from an actor class?

**URL:** <https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716>\
**Category:** Developers\
**Created:** [February 5, 2022, 4:05pm UTC](https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716 "2022-02-05T16:05:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [February 5, 2022, 4:05pm UTC](https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716/1 "2022-02-05T16:05:53Z")

</div>

I’m trying to retrieve the principal of an actor class from within the actor class. Below is the code that I wrote… and below that is the error that I’m getting when trying to compile this code:

```auto
func userAccountId() : Account.AccountIdentifier {
        let canisterId = Principal.fromActor(This);
        Account.accountIdentifier(canisterId, Account.defaultSubaccount())
    };

```

Error message:

```auto
type error [M0057], unbound variable This

```

I’ve tried making `This` and `this` as the argument for `Principal.fromActor()` and both are throwing the same error.

---

<div class="post-metadata">

**Author:** ![inBlockit](https://avatars.discourse-cdn.com/v4/letter/i/13edae/32.png) [@inBlockit](https://forum.dfinity.org/u/inBlockit)\
**Post date:** [February 5, 2022, 5:09pm UTC](https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716/2 "2022-02-05T17:09:40Z")

</div>

I believe that you have to bind `this` during instantiation of that actor. Check out how this is done in WhoAmI example in motoko playground. [MotokoPlayground link](https://m7sm4-2iaaa-aaaab-qabra-cai.raw.ic0.app)

```auto
shared (install) actor class WhoAmI(someone : Principal) = 
  this { // Bind the optional `this` argument (any name will do)

  public func idQuick() : async Principal {
    return Principal.fromActor(this);
  };
}

```

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [February 5, 2022, 5:32pm UTC](https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716/3 "2022-02-05T17:32:53Z")

</div>

It’s actually a bit easier - you can just do

```auto
Principal.fromActor(WhoAmI)

```

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [February 5, 2022, 5:36pm UTC](https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716/4 "2022-02-05T17:36:00Z")

</div>

When I try it the way you suggested @anon74414410 , I’m getting the following error message:

```auto
type error [M0056], variable WhoAmI is in scope but not available in compiled code

```

I think this way would work with an actor, but not for an actor class, I guess?

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [February 5, 2022, 5:54pm UTC](https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716/5 "2022-02-05T17:54:24Z")

</div>

It’s available at runtime, so you can query it during your public function calls

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [February 5, 2022, 6:30pm UTC](https://forum.dfinity.org/t/how-do-i-retrieve-the-principal-from-an-actor-class/10716/6 "2022-02-05T18:30:51Z")

</div>

This only works for `actor WhoAmI`, not `actor class WhoAmI()`. For actor class, you need to bind the whole class, e.g. `actor class WhoAmI() = WhoAmI { ... }`
