# Steps to Create a Java File source: https://developer.mastercard.com/donations/tutorial/sample-project-creation/makingapicall/index.md ## Create a Java file, {#create-a-java-file} Create a Java file called **CardsApiClient.java** , at the path **src/main/java/** .
To make it easy to understand, only the snippets needed to make the API call is shown above. The full content of **CardsApiClient.java** is shown in these steps: 1. To make the API call, you need to make your OAuth credentials available to the program to use: * Oauth ```Oauth String consumerKey = "your consumer key"; String signingKeyFilePath = "/path/to/your/key.p12"; String signingKeyAlias = "your key alias"; String signingKeyPassword = "your password"; PrivateKey signingKey = AuthenticationUtils.loadSigningKey(signingKeyFilePath, signingKeyAlias, signingKeyPassword); ``` 2. Afterwards, you instantiate a client that you can set up to sign requests that you send with your authentication credentials. * Client_Instantiation ```Client_Instantiation ApiClient client = new ApiClient(); String endpoint = "https://sandbox.api.mastercard.com/donations/donors" + donorId + "/cards"; client.setBasePath(endpoint); client.setDebugging(true); client.setReadTimeout(40000); List interceptors = client.getHttpClient().networkInterceptors(); interceptors.add(new ForceJsonResponseInterceptor()); interceptors.add(new OkHttp2OAuth1Interceptor(consumerKey, signingKey)); CardsApi api = new CardsApi(client); ``` 3. The interceptor that you use to sign requests is also provided by the Mastercard OAuth library. * Interceptor ```Interceptor private static class ForceJsonResponseInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request(); String withJsonFormatUrl = withJsonFormat(originalRequest.uri().toString()); Request newRequest = originalRequest.newBuilder().url(withJsonFormatUrl).build(); return chain.proceed(newRequest); } private String withJsonFormat(String uri) { StringBuilder newUri = new StringBuilder(uri); newUri.append(uri.contains("?") ? "&" : "?"); newUri.append("Format=JSON"); return newUri.toString(); } } ``` 4. You need to have donor and card basic details to call the `addCard` API. * Interceptor ```Interceptor String donorId = "your donorId"; private static AddCard getAddCardRequest() { AddCard addCard = new AddCard(); CardBillingAddress cardBillingAddress = new CardBillingAddress(); addCard.setPrimaryAccountNumber("Enter your account number"); addCard.setCardholderName("Enter your cardholder name"); addCard.setCardAlias("Enter your cardAlias name"); cardBillingAddress.setLine1("Enter your line1 address"); cardBillingAddress.setLine2("Enter your line2 address"); cardBillingAddress.setLine3("Enter your line3 address"); cardBillingAddress.setCity("Enter your city"); cardBillingAddress.setState("Enter your state"); cardBillingAddress.setCountryCode("Enter your country"); cardBillingAddress.setPostalCode("Enter your postal Code"); addCard.setBillingAddress(cardBillingAddress); return addCard; } ``` 5. With your environment set up, you can start to build a request. Any nested objects within the Request Body have each been assigned a wrapper class, and so in order to build the request, you will be instantiating multiple objects and ultimately wrapping those with the main request object. 6. You make the API call and then store the response in a Response object. Once you have the response stored, you have different options, but for this tutorial you can simply use `toString` to print the response body. Note: Well Done! 👏. You have successfully completed the tutorial on how to generate the SDK and connect to the Mastercard Donate API in sandbox using the SDK ### Content of CardsApiClient.java {#content-of-cardsapiclientjava} * Interceptor ```Interceptor package com.mastercard.donationapis; import com.mastercard.developer.interceptors.OkHttp2OAuth1Interceptor; import com.mastercard.developer.utils.AuthenticationUtils; import com.squareup.okhttp.Interceptor; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import org.openapitools.client.ApiClient; import org.openapitools.client.api.CardsApi; import org.openapitools.client.model.AddCard; import org.openapitools.client.model.CardBillingAddress; import org.openapitools.client.model.DonateCard; import java.io.IOException; import java.security.PrivateKey; import java.util.List; public class CardsApiClient { public static void main(String[] args) throws Exception { ApiClient client = new ApiClient(); String consumerKey = "your consumer key"; String signingKeyFilePath = "path to your key.p12"; String signingKeyAlias = "your key alias"; String signingKeyPassword = "your key store password"; String donorId = "your donorId"; PrivateKey signingKey = AuthenticationUtils.loadSigningKey(signingKeyFilePath, signingKeyAlias, signingKeyPassword); String endpoint = "https://sandbox.api.mastercard.com/donations/donors" + donorId + "/cards"; client.setBasePath(endpoint); client.setDebugging(true); client.setReadTimeout(40000); List interceptors = client.getHttpClient().networkInterceptors(); interceptors.add(new ForceJsonResponseInterceptor()); interceptors.add(new OkHttp2OAuth1Interceptor(consumerKey, signingKey)); CardsApi api = new CardsApi(client); api.setApiClient(client); DonateCard response = api.addCardUsingPOST(donorId,getAddCardRequest()); } private static AddCard getAddCardRequest() { AddCard addCard = new AddCard(); CardBillingAddress cardBillingAddress = new CardBillingAddress(); addCard.setPrimaryAccountNumber("Enter your account number"); addCard.setCardholderName("Enter your cardholder name"); addCard.setCardAlias("Enter your cardAlias name"); cardBillingAddress.setLine1("Enter your line1 address"); cardBillingAddress.setLine2("Enter your line2 address"); cardBillingAddress.setLine3("Enter your line3 address"); cardBillingAddress.setCity("Enter your city"); cardBillingAddress.setState("Enter your state"); cardBillingAddress.setCountryCode("Enter your country"); cardBillingAddress.setPostalCode("Enter your postal Code"); addCard.setBillingAddress(cardBillingAddress); return addCard; } // Add "Format=JSON" to the request for the service/gateway to return a JSON response. private static class ForceJsonResponseInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request(); String withJsonFormatUrl = withJsonFormat(originalRequest.uri().toString()); Request newRequest = originalRequest.newBuilder().url(withJsonFormatUrl).build(); return chain.proceed(newRequest); } private String withJsonFormat(String uri) { StringBuilder newUri = new StringBuilder(uri); newUri.append(uri.contains("?") ? "&" : "?"); newUri.append("Format=JSON"); return newUri.toString(); } } } ```