# Making an API Call source: https://developer.mastercard.com/pay-with-rewards/tutorial/api-testing/step3/index.md ## Making an API Call {#making-an-api-call} 1. Under the `src/main/java/` folder path, create a Java file named **ApiClientConfiguration.java**. 2. To make the API call, you need to make your OAuth credentials available to the program for use. * A PrivateKey is created with the provided credentials for later use. * AuthenticationUtils is provided by the Mastercard OAuth signing library. ```java // Read these values from application.properties file. @Value("${mastercard.api.key.alias}") private String signingKeyAlias; @Value("${mastercard.api.keystore.password}") private String signingKeyPassword; @Value("${mastercard.api.p12.path}") private String signingKeyPkcs12FileName; ``` You can include the encryption \& signing information in the application's application.properties file as shown below ```java # path to keystore (.p12). This .p12 file will be downloaded during project creation in Mastercard Developers. mastercard.api.p12.path=src/main/resources/pay-with-rewards.p12 # Consumer key. Copy this from "Sandbox/Production Keys" on your project page in https://developer.mastercard.com/dashboard. mastercard.api.consumer.key=XYzbhDCSeNYvJpLL5l028sWL9it739PYh6LUd4Zja15xcRpY!fd209e6c579dc9d7be52da93d35ae6b6c167c174690bDsfa # key alias. Default key alias for sandbox is "keyalias" (without quotes). mastercard.api.key.alias=keyalias # keystore password. Default keystore password for sandbox is "keystorepassword" (without quotes) mastercard.api.keystore.password=keystorepassword # basepath. basePath=https://sandbox.api.mastercard.com/loyalty/mrs # path to certificate (PEM) file whose public key will be used for encryption. Download this from "Client Encryption Keys" under Actions dropdown on your project page in https://developer.mastercard.com mastercard.api.encryption.pem.path=src/main/resources/pay-with-rewards-encrypt.pem # The SHA-256 hex-encoded digest of the key used for encryption. Copy this from "Client Encryption Keys" under FINGERPRINT section on your project page in https://developer.mastercard.com mastercard.api.encryption.key.fingerprint=a36a78ddb31a3bb01e6e2887097a53ee85e51755a0db6e9ea56c4b5780182598 springdoc.paths-to-exclude=/swagger-resources/** ``` Tip: Please refer to the following [tutorial](https://developer.mastercard.com/pay-with-rewards/tutorial/sandbox-access/index.md) to generate the keys that are used in the application.properties file. * A certificate object can be created from a file by calling EncryptionUtils.loadEncryptionCertificate. Supported certificate formats are PEM, DER. Certificate encryptionCertificate = EncryptionUtils.loadEncryptionCertificate(""); * Entire payloads can be encrypted using the "$" operator as encryption path ```java jweConfig = JweConfigBuilder.aJweEncryptionConfig() .withEncryptionCertificate(encryptionCertificate) .withEncryptionPath("$", "$") .withEncryptionKeyFingerprint(encryptionKeyFingerprint) .withEncryptedValueFieldName("encryptedPayload") .build(); ```
* Instantiate a client with your authentication credentials and configuration object as interceptors before you can send it. ```java PrivateKey signingKey = AuthenticationUtils.loadSigningKey(signingKeyPkcs12FileName, signingKeyAlias, signingKeyPassword); apiClient = new ApiClient(); apiClient.setBasePath("https://sandbox.api.mastercard.com"); apiClient.setDebugging(true); OkHttpClient.Builder okHttpClientBuilder = apiClient.getHttpClient().newBuilder(); okHttpClientBuilder.addInterceptor(new OkHttpOAuth1Interceptor(consumerKey, signingKey)); return apiClient.setHttpClient(okHttpClientBuilder.build()); ``` Note: For conciseness, only the barebones of what is needed to make the API call is shown above. The full contents of ApiClientConfiguration.java is shown below. ### Contents of ApiClientConfiguration.java {#contents-of-apiclientconfigurationjava} ```java package com.mastercard.developer.config; import com.mastercard.developer.interceptors.OkHttpOAuth1Interceptor; import com.mastercard.developer.utils.AuthenticationUtils; import lombok.extern.slf4j.Slf4j; import okhttp3.OkHttpClient; import org.openapitools.client.ApiClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.mastercard.developer.encryption.JweConfig; import com.mastercard.developer.encryption.JweConfigBuilder; import com.mastercard.developer.interceptors.OkHttpJweInterceptor; import com.mastercard.developer.utils.EncryptionUtils; import java.security.cert.Certificate; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import java.security.PrivateKey; @Service @Slf4j public class ApiClientConfiguration { @Value("${basePath}") private String basePath; @Value("${mastercard.api.consumer.key}") public String consumerKey; @Value("${mastercard.api.key.alias}") private String signingKeyAlias; @Value("${mastercard.api.keystore.password}") private String signingKeyPassword; @Value("${mastercard.api.p12.path}") private String signingKeyPkcs12FileName; @Value("${mastercard.api.encryption.pem.path}") private String encryptionKeyPemFileName; @Value("${mastercard.api.encryption.key.fingerprint}") private String encryptionKeyFingerprint; private ApiClient apiClient = null; @Autowired private HttpServletRequest httpRequest; public ApiClient getApiClient() { if (httpRequest.getMethod().equals("POST")) { createNewApiClient(true); } else { createNewApiClient(false); } return apiClient; } public ApiClient createNewApiClient(boolean encryptRequest) { apiClient = new ApiClient(); JweConfig jweConfig = null; try { /* The following code will encrypt the payload if the payload has sensitive information */ if (encryptRequest) { Certificate encryptionCertificate = EncryptionUtils.loadEncryptionCertificate(encryptionKeyPemFileName); jweConfig = JweConfigBuilder.aJweEncryptionConfig() .withEncryptionCertificate(encryptionCertificate) .withEncryptionPath("$", "$") .withEncryptionKeyFingerprint(encryptionKeyFingerprint) .withEncryptedValueFieldName("encryptedPayload") .build(); } PrivateKey signingKey = AuthenticationUtils.loadSigningKey(signingKeyPkcs12FileName, signingKeyAlias, signingKeyPassword); apiClient.setBasePath(basePath); apiClient.setDebugging(true); OkHttpClient.Builder okHttpClientBuilder = apiClient.getHttpClient().newBuilder(); if (encryptRequest) { okHttpClientBuilder.addInterceptor(new OkHttpJweInterceptor(jweConfig)); } okHttpClientBuilder.addInterceptor(new OkHttpOAuth1Interceptor(consumerKey, signingKey)); return apiClient.setHttpClient(okHttpClientBuilder.build()); } catch (Exception e) { log.error(e.getMessage()); } return apiClient; } } ``` In the above block of code, the request is signed using the parameters from the application.properties file. The request is also encrypted if it's a POST call. Refer [API Basics](https://developer.mastercard.com/pay-with-rewards/documentation/api-basics/index.md) for signing and encryption details. * Now put everything together, and you can start using the APIs by making use of the generated classes. ```java @ApiOperation(value = "Search User Profile Call", notes = "Search User Profile", tags = { "redeemers" }) @PostMapping("/redeemers/search") public RedeemerProfile searchUserProfile(@RequestBody SearchProfileRequest request) { RedeemerProfile response; try { log.info("Method : searchUserProfile, Message : Searching user profile with the user id :" + request.getUserId()); ApiClient apiClient = apiClientConfiguration.createNewApiClient(true); RedeemersProfileApi redeemersProfileApi = getRedeemersProfileApi(apiClient); response = redeemersProfileApi.searchRedeemersProfile(request); if (response != null) { log.info("Method : searchUserProfile, Message :Successfully found the user profile from using search " + response); return response; } } catch (ApiException e) { log.error("Method : searchUserProfile, Message : Failed to search the user profile with user id :" + request.getUserId()); throw new InvalidRequest(e.getMessage(), e.getResponseBody()); } throw new InvalidRequest(HttpStatus.BAD_REQUEST); } public RedeemersProfileApi getRedeemersProfileApi(ApiClient apiClient) { return new RedeemersProfileApi(apiClient); } } ``` Tip: Refer to the following [reference app](https://developer.mastercard.com/pay-with-rewards/documentation/reference-app/index.md) for more details.