# How to get the user which is calling a microservice API endpoint

**URL:** <https://community.cumulocity.com/t/how-to-get-the-user-which-is-calling-a-microservice-api-endpoint/7409>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [August 24, 2022, 12:32pm UTC](https://community.cumulocity.com/t/how-to-get-the-user-which-is-calling-a-microservice-api-endpoint/7409 "2022-08-24T12:32:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kai\_Sieben](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/kai_sieben/32/5541_2.png) [@Kai\_Sieben](https://community.cumulocity.com/u/Kai_Sieben)\
**Post date:** [August 24, 2022, 12:32pm UTC](https://community.cumulocity.com/t/how-to-get-the-user-which-is-calling-a-microservice-api-endpoint/7409/1 "2022-08-24T12:32:00Z")

</div>

#### What product/components do you use and which version/fix level are you on?

Cumulocity 1011.0.28

#### Is your question related to the free trial, or to a production (customer) instance?

production

#### What are you trying to achieve? Please describe it in detail.

Within the microservice SDK, how could we get the username of the user which is calling a Microservice API endpoint?

Thanks!

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [August 24, 2022, 1:04pm UTC](https://community.cumulocity.com/t/how-to-get-the-user-which-is-calling-a-microservice-api-endpoint/7409/2 "2022-08-24T13:04:35Z")

</div>

Hi Kai,

not sure if this is outdated but this is how I did this some years ago in my REST Controller:

```auto
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
SecurityUserDetails securityUserDetails = ((SecurityUserDetails)auth.getPrincipal());
securityUserDetails.getUsername();

```

---

<div class="post-metadata">

**Author:** ![Philipp\_Emmel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/philipp_emmel/32/19455_2.png) [@Philipp\_Emmel](https://community.cumulocity.com/u/Philipp_Emmel)\
**Post date:** [August 25, 2022, 10:50am UTC](https://community.cumulocity.com/t/how-to-get-the-user-which-is-calling-a-microservice-api-endpoint/7409/3 "2022-08-25T10:50:53Z")

</div>

This is the generic Spring based way to access calling user details. If you prefer using our SDK, or if you want an easy way to use these credentials for API calls you can inject a bean of type **ContextService\<UserCredentials\>** and get the details from there.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [February 21, 2023, 11:39am UTC](https://community.cumulocity.com/t/how-to-get-the-user-which-is-calling-a-microservice-api-endpoint/7409/5 "2023-02-21T11:39:22Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
