# Integration and Testing source: https://developer.mastercard.com/identity-insights-for-accounts/documentation/tutorials-and-guides/api-testing-tutorial/index.md ### Introduction {#introduction} In this section, we'll explain how to generate Identity Insights for Accounts API client to use with MTLS-enabled APIs. ### What you will learn {#what-you-will-learn} * Generate an API client using [OpenAPI Generator](https://openapi-generator.tech/), which provides generators and library templates for supporting multiple languages and frameworks. * Configure the generated API client to use your certificate for authentication. ### Estimated time to complete this tutorial {#estimated-time-to-complete-this-tutorial} * 30 minutes ### Prerequisites {#prerequisites} To complete this tutorial, you will need: * An IDE of your choice * Mastercard Developers account with access to the Identity Insights for Accounts API * Identity Insights for Accounts API OpenAPI Specification (Refer to the [Open Specification](https://developer.mastercard.com/identity-insights-for-accounts/documentation/api-reference/index.md) for details.) Note: We use Java for code samples in this guide, but OpenAPI Generator supports building clients for most major languages. ## Create an API Project in Mastercard Developers {#create-an-api-project-in-mastercard-developers} Please refer sandbox tutorial [here](https://developer.mastercard.com/identity-insights-for-accounts/documentation/tutorials-and-guides/sandbox-access-tutorial/index.md) for creating an API. Refer to the following page for more information on [Using MTLS to Access Mastercard API Client](https://developer.mastercard.com/platform/documentation/security-and-authentication/using-mtls-to-access-mastercard-apis/). ## Download an OpenAPI Specification {#download-an-openapi-specification} * Access the documentation for the chosen API. * Click to open the API reference specification page [here](https://developer.mastercard.com/identity-insights-for-accounts/documentation/api-reference/index.md). * For this tutorial, we'll use the Swagger documentation for the Identity Insights for Accounts API [IIA-swagger-public.yaml](https://static.developer.mastercard.com/content/identity-insights-for-accounts/swagger/IIA-swagger-public.yaml) (30KB) ### Generate an API Client Project {#generate-an-api-client-project} * Download the OpenAPI Generator CLI (you can follow the instructions [here](https://openapi-generator.tech/docs/installation/)) * Create a `config.json` file with various options for the OpenAPI Generator. * Java * Python * NodeJs * C# ```Java { "groupId" : "com.mastercard.idv", "artifactId" : "aii-service-reference-app", "artifactVersion" : "0.0.1-SNAPSHOT", "invokerPackage" : "com.mastercard.idv", "apiPackage" : "com.mastercard.idv.aii" } ``` ```Python {} ``` ```NodeJs { "useES6" : false } ``` ```C# { "targetFramework" : "netstandard2.1", "packageName" : "com.mastercard.idv.tra.Service", "packageVersion" : "1.0.0", "sourceFolder" : "", "netCoreProjectFile": true, "modelPropertyNaming":"camelCase", "optionalAssemblyInfo": false } ```
* Execute the following command for the language in which you want the client to be generated (we assume here the `openapi-generator-cli` command is available): * * Java * Python * NodeJs * C# ```Java openapi-generator-cli generate -g java --library okhttp-gson -i *.yaml -c config.json -o api_client ``` ```Python If using openapi generator version >= 5.0.0, use below command to generate the client: openapi-generator-cli generate -g python-legacy -i *.yaml -c config.json -o api_client "> # If using openapi generator version < 5.0.0, use below command to generate the client: openapi-generator-cli generate -g python -i *.yaml -c config.json -o api_client # If using openapi generator version >= 5.0.0, use below command to generate the client: openapi-generator-cli generate -g python-legacy -i *.yaml -c config.json -o api_client ``` ```NodeJs openapi-generator-cli generate -g javascript -i *.yaml -c config.json -o api_client ``` ```C# openapi-generator-cli generate -g csharp-netcore -i *.yaml -c config.json -o api_client ``` * Look at the `api_client` directory for the generated project Tip: More options are available. The OpenAPI Generator CLI documentation can be found [here](https://openapi-generator.tech/docs/usage/) ### Create an API Client Instance and Enable Authentication {#create-an-api-client-instance-and-enable-authentication} 1. Create a test project or an application project 2. Create an instance of the generated `ApiClient` class and configure it as below: * Java ```Java ApiClient client = new ApiClient(); OkHttpClient.Builder httpClientBuilder = client.getHttpClient().newBuilder(); // Configure the Mastercard service URL client.setBasePath("https://sandbox.idv.mastercard.com/identity/accounts"); // Load your client certificate KeyStore pkcs12KeyStore = KeyStore.getInstance("PKCS12"); pkcs12KeyStore.load(new FileInputStream("./path/to/your/client-cert.p12"), "keystorepassword".toCharArray()); // Configure a secure socket KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(pkcs12KeyStore, "keystorepassword".toCharArray()); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(pkcs12KeyStore); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom()); client.setHttpClient( httpClientBuilder .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagerFactory.getTrustManagers()[0]) .build() ); ``` * Replace: * `./path/to/your/client-cert.p12` with the path to your client certificate. * `keystorepassword` with the password for your client certificate. * `https://sandbox.idv.mastercard.com/identity/accounts` with the actual environment and service path you want to call. Note: OpenAPI Generator supports several HTTP clients and frameworks for Java (Jersey, OkHttp, RestTemplate, Retrofit). In this guide, we chose to use `okhttp-gson`. Tip: Refer to the [reference app](https://developer.mastercard.com/identity-insights-for-accounts/documentation/reference-app/index.md) for more details. ### Call the Service {#call-the-service} * Under the src/main/resources/ folder path, create a property file named application.properties. * To make the API call, you need to make your MTLS credentials available to the program for use ```XML Resource jweEncPublicKeyPath = classpath:publicKey.pem; String jweClientEncPublicKey = "FingerPrint from mastercard portal"; String clientside = "classpath:idvhub-test-external-client.mastercard.int.pfx"; PrivateKey truststore = "classpath:cacerts"; ``` * Instantiate a client with your authentication credentials and configuration object as interceptors before you can send it. * Java ```Java @Bean(value = "aiiApiClient") public com.mastercard.idv.serviceaii.ApiClient aiiApiClient() throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException { com.mastercard.idv.serviceaii.ApiClient client = new com.mastercard.idv.serviceaii.ApiClient(); client.setBasePath(baseUrl); client.setHttpClient(createHttpClient(client.getHttpClient())); return client; } ``` Tip: Refer to the [reference app](https://developer.mastercard.com/identity-insights-for-accounts/documentation/reference-app/index.md) for more details.