# Get Application ID or Managed Object ID of Cumulocity Microservice

**URL:** <https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829>\
**Category:** Forum\
**Tags:** cumulocity, microservice\
**Created:** [October 25, 2022, 5:05pm UTC](https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829 "2022-10-25T17:05:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Winter1](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Christian\_Winter1](https://community.cumulocity.com/u/Christian_Winter1)\
**Post date:** [October 25, 2022, 5:05pm UTC](https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829/1 "2022-10-25T17:05:13Z")

</div>

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

Cumulocity IoT; I guess the exact version is irrelevant here.

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

I’m implementing a Cumulocity microservice in Java. Is it possible to access the associated Application ID of a microservice (the ID `<app_id>` used in `<URL>/application/applications/<app_id>`) from within the microservice? Or is it possible to access the Managed Objects associated with the deployed microservice (stored in the inventory at `<URL>/inventory/managedObjects/`; esp. the Managed Object of type `c8y_Application_<app_id>` would be desired, but type `c8y_microservice_manifest_<app_id>` could be okay as well)? I’m trying to use the identity of the microservice itself for setting this as source of events etc. Currently, I’m hardcoding IDs, but I hope that a microservice can somehow retrieve it’s own ID programmatically.

---

<div class="post-metadata">

**Author:** ![Harald\_Meyer](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/harald_meyer/32/1959_2.png) [@Harald\_Meyer](https://community.cumulocity.com/u/Harald_Meyer)\
**Post date:** [October 25, 2022, 5:21pm UTC](https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829/2 "2022-10-25T17:21:05Z")

</div>

Hi Christian,

a microservice can call this endpoint:  
[https://cumulocity.com/api/10.14.0/#tag/Current-application](https://cumulocity.com/api/10.14.0/#tag/Current-application)

to receive information about itself. Is the contained information sufficient for your purposes?

Harald

---

<div class="post-metadata">

**Author:** ![Christian\_Winter1](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Christian\_Winter1](https://community.cumulocity.com/u/Christian_Winter1)\
**Post date:** [October 28, 2022, 7:35pm UTC](https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829/3 "2022-10-28T19:35:04Z")

</div>

Dear Harald, that’s a perfect fit to what I sought. Thank you! Best regards, Christian

---

<div class="post-metadata">

**Author:** ![Christian\_Winter1](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Christian\_Winter1](https://community.cumulocity.com/u/Christian_Winter1)\
**Post date:** [October 28, 2022, 7:54pm UTC](https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829/4 "2022-10-28T19:54:36Z")

</div>

For those who want to address the “current application” REST endpoint via Java (like me), here is a lean implementation utilizing the Cumulocity Java SDK:

```java
import org.springframework.beans.factory.annotation.Autowired;

import com.cumulocity.microservice.context.ContextService;
import com.cumulocity.microservice.context.credentials.MicroserviceCredentials;
import com.cumulocity.microservice.subscription.model.core.PlatformProperties;
import com.cumulocity.microservice.subscription.repository.application.ApplicationApi;
import com.cumulocity.rest.representation.application.ApplicationRepresentation;

@Autowired
private ContextService<MicroserviceCredentials> contextService;
@Autowired
private PlatformProperties platformProperties;
@Autowired
private ApplicationApi appApi;

public ApplicationRepresentation getCurrentApplication() {
	return contextService.callWithinContext((MicroserviceCredentials)platformProperties.getMicroserviceBoostrapUser(), () -> {
		return appApi.currentApplication().get();
	});
}

```

The `ApplicationAPI` class wraps the relevant REST interface. The other pieces of the code are used for accessing the endpoint with the right permissions.

---

<div class="post-metadata">

**Author:** ![Christian\_Winter1](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Christian\_Winter1](https://community.cumulocity.com/u/Christian_Winter1)\
**Post date:** [November 23, 2022, 7:01pm UTC](https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829/5 "2022-11-23T19:01:22Z")

</div>

Here are some code snippets for accessing the Managed Object associated with an application (the Managed Object with type `c8y_Application_<app_id>`) on top of the ApplicationRepresentation object as described in the previous post. But first two notes:

1. A microservice only has such a Managed Object if it is deployed on Cumulocity. The object does not exist if the microservice is executed locally (test execution). You need to create a suitable dummy Managed Object in this case for using the code below. However, such dummy objects will be removed automatically from time to time (presumably, Cumulocity considers these objects to be inconsistent with the deployment status of the microservice).
2. Be aware that the Managed Object is associated with a specific Cumulocity tenant. This is especially important for microservices running as multi-tenant service. Because of this, the Managed Object must be accessed only in tenant scope.

If you want to access the Managed Object just within a single procedure, you can do it for example as follows:

```java
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;

import com.cumulocity.microservice.subscription.service.MicroserviceSubscriptionsService;
import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;
import com.cumulocity.sdk.client.Platform;
import com.cumulocity.sdk.client.inventory.InventoryFilter;

@Autowired
private Platform platform;
@Autowired
private MicroserviceSubscriptionsService msSubscriptions;

public void doWhatever() {
	msSubscriptions.subscribe();
	msSubscriptions.runForEachTenant(() -> {
		ManagedObjectRepresentation appMO = getAppMO(getCurrentApplication().getId());
		System.out.println(appMO);
		// do whatever with the appMO, e.g. use it as source for an event or alarm
	});
}

public ManagedObjectRepresentation getAppMO(String appId) {
	InventoryFilter filter = new InventoryFilter();
	filter.byType("c8y_Application_" + appId);
	Iterator<ManagedObjectRepresentation> i = platform.getInventoryApi().getManagedObjectsByFilter(filter).get().iterator();
	ManagedObjectRepresentation mo = i.hasNext() ? i.next() : null;
	if (i.hasNext())
		log.warn("Multiple Managed Objects for Application " + appId + " exist in the Inventory. Using the first one returned.");
	return mo;
}

```

If you want to access the Managed Object from multiple procedures, a Bean with tenant scope might be handy:

```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import com.cumulocity.microservice.context.inject.TenantScope;
import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;

@Autowired
@Qualifier("currentAppMO")
private ManagedObjectRepresentation appMO;

@Bean
@TenantScope
public ManagedObjectRepresentation currentAppMO() {
	return getAppMO(getCurrentApplication().getId());
}

```

---

<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:** [May 22, 2023, 7:01pm UTC](https://community.cumulocity.com/t/get-application-id-or-managed-object-id-of-cumulocity-microservice/7829/6 "2023-05-22T19:01:23Z")

</div>

Closed on May 22, 2023, 7:01pm UTC

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