Alexa Service Clients
The SDK includes service clients that you can use to call Alexa APIs from within your skill logic.
Creating a service client instance
Service clients can be used in any request handler, exception handler, and request and response interceptor. The ServiceClientFactory
contained inside the HandlerInput
allows you to retrieve client instances for every supported Alexa service.
The following example shows the handle
method of a request handler that creates an instance of the DirectiveService
client. Creating a service client instance is as simple as calling the appropriate factory method.
@Override
public Optional<Response> handle(HandlerInput input) {
DirectiveService directiveServices = input.getServiceClientFactory().getDirectiveService();
directiveServices.enqueue(SendDirectiveRequest.builder().build());
// other handler logic goes here
}
Once created, you can use the service client instance to call the API operations exposed by the service.
EndpointEnumerationService
Skills can use EndpointEnumerationService
to retrieve the user's connected endpoints that the skill can interact with.
Interface
public interface EndpointEnumerationService {
EndpointEnumerationResponse getEndpoints() throws ServiceException;
}
Code Sample
The following example shows a function that gets the connected endpoints.
/**
* Get the connected endpoint ID that matches the specified friendly name.
* @param input - The data provided to all RequestHandlers from Alexa Service.
* @param friendlyName - Optional. The endpoint friendly name.
* @return The endpoint ID. Null if not available
*/
private String getConnectedGadgetWithServiceClient(HandlerInput input, String friendlyName) {
EndpointEnumerationResponse response = input.getServiceClientFactory()
.getEndpointEnumerationService()
.getEndpoints();
List<EndpointInfo> endpoints = response.getEndpoints();
// Returns the first enumerated endpoint if the friendly name is not specified
if (friendlyName == null && !endpoints.isEmpty()) {
return endpoints.get(0).getEndpointId();
}
// Returns the endpoint that matches the friendly name
for (EndpointInfo endpoint : endpoints) {
if (endpoint.getFriendlyName().equalsIgnoreCase(friendlyName)) {
return endpoint.getEndpointId();
}
}
return null;
}
Last updated: Nov 28, 2023