# Generate and Configure an API Client
source: https://developer.mastercard.com/mastercard-developers-api/documentation/tutorials-and-guides/generate-an-api-client/index.md

###### Time to complete: *20 minutes*

## Overview {#overview}

In this tutorial, we will explain how to generate a simple API client for consuming the Mastercard Developers API.

You will learn how to:

* Generate an API client using [OpenAPI Generator](https://openapi-generator.tech/), which provides generators and library templates for supporting multiple languages and frameworks
* Integrate with our client libraries to sign requests.

<br />

**Language used in this tutorial:** Java

## Step-By-Step Guide {#step-by-step-guide}

1. Log in to your Mastercard Developers account.
2. Access your Account page and click **Add key** in the Developers API Keys section and follow the step-by-step instructions.
3. Download your [signing key](https://developer.mastercard.com/platform/documentation/security-and-authentication/using-oauth-1a-to-access-mastercard-apis/#the-signing-key%22) as a PKCS#12 keystore.
4. Get your client [consumer key](https://developer.mastercard.com/platform/documentation/security-and-authentication/using-oauth-1a-to-access-mastercard-apis/#the-consumer-key) from the Developers API Keys section of your Account page.

Note: Well done! You now have the credentials to call the API.

*** ** * ** ***

For this tutorial, we will use
[mastercard_developers_api.yaml](https://static.developer.mastercard.com/content/mastercard-developers-api/swagger/mastercard_developers_api.yaml) (153KB)

*** ** * ** ***

1. Download the OpenAPI Generator CLI (you can follow the instructions [here](https://openapi-generator.tech/docs/installation))

2. Create a `config.json` file with some options for OpenAPI Generator:

   * Java

   ```json
   { 
       "groupId" : "com.acme.app",
       "artifactId" : "mastercard-developers-api-client",
       "artifactVersion" : "1.0.0",
       "invokerPackage" : "com.acme.app.mastercard_developers",
       "apiPackage" : "com.acme.app.mastercard_developers.api",
       "modelPackage" : "com.acme.app.mastercard_developers.model"
   }
   ```

   <br />

3. 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

   ```sh
   openapi-generator-cli generate -g java -i *.yaml -c config.json -o api_client
   ```

   <br />

4. 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)

*** ** * ** ***

The Mastercard [client authentication libraries](https://github.com/Mastercard?&q=oauth1) provide you with some request interceptor classes you can use to configure your generated API client. These classes will take care of adding the correct *Authorization* header before sending the requests.

To continue, add the package below to the generated project. Browse the links below to find out how to install the library using your favorite package manager:

|                      |                                                           ![Java](https://static.developer.mastercard.com/content/platform/img/java.svg)                                                           |                                                                             |                                                                             |                                                                             |                                                                             |                                                                             |   |
|----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------|---|
| **Download/install** | [![](https://img.shields.io/maven-central/v/com.mastercard.developer/oauth1-signer.svg?style=flat&color=f99f1c&label=)](https://search.maven.org/artifact/com.mastercard.developer/oauth1-signer/) | ![](https://static.developer.mastercard.com/content/platform/img/blank.png) | ![](https://static.developer.mastercard.com/content/platform/img/blank.png) | ![](https://static.developer.mastercard.com/content/platform/img/blank.png) | ![](https://static.developer.mastercard.com/content/platform/img/blank.png) | ![](https://static.developer.mastercard.com/content/platform/img/blank.png) |   |

*** ** * ** ***

1. Create a test project or an application project
2. Locate the consumer key and signing key you got from ***Step 1: Generate API Credentials***.
3. Create an instance of the generated `ApiClient` class and configure it as below:

```java
ApiClient client = new ApiClient();
OkHttpClient.Builder httpClientBuilder = client.getHttpClient().newBuilder();

// Load the signing key
PrivateKey signingKey = AuthenticationUtils.loadSigningKey("./path/to/your/signing-key.p12", "keyalias", "keystorepassword");

// Add the interceptor code responsible for signing HTTP requests
httpClientBuilder.addInterceptor(new OkHttpOAuth1Interceptor("000000000000000000000000000000000000000000000000!000000000000000000000000000000000000000000000000", signingKey));
// ...
client.setHttpClient(httpClientBuilder.build());

// Configure the Mastercard service URL
client.setBasePath("https://apiedge.mastercard.com/developers");

// Add some more logs
client.setDebugging(true);
```

4. Replace:

* `0000(...)0000` with your consumer key string
* `./path/to/your/signing-key.p12` with your signing key file

Note: OpenAPI Generator supports several HTTP clients and frameworks for Java (Jersey, OkHttp, RestTemplate, Retrofit, ...). In this tutorial, we chose to use `okhttp-gson`. Refer to our Java library on [GitHub](https://github.com/Mastercard/oauth1-signer-java#integrating-with-openapi-generator-api-client-libraries) for the list of supported options.

*** ** * ** ***

We can now send requests to the Mastercard service. Let's try for instance the `/projects` endpoint:


API Reference: `POST /projects`

<br />

For that, use the generated `ProjectsApi` class, as in the following code:
* Java

```java
ProjectsApi api = new ProjectsApi(client);
NewProjectPartnerCredential credential = new NewProjectPartnerCredential();
credential.setDescription("A test credential");
credential.setType("PARTNER");
NewProject newProject = new NewOpenBankingPartnerProject()
    .region("US")
    .credential(credential)
    .type("OPEN_BANKING_PARTNER")
    .name("My first Open Finance project")
    .environment("SANDBOX")
    .service(new NewProjectService().serviceId(1443L)); // Mastercard Open Finance API ID
Project project = api.createProject(newProject);
UUID projectId = project.getId();
System.out.printf("Project created. UUID: %s%n", projectId);
```

<br />

Now, let's try to add a Production environment to the project by using the `/projects/{project_id}/environments` endpoint:


API Reference: `POST /projects/{project_id}/environments`

<br />

* Java

```java
NewProjectCredential credential = new NewProjectPartnerCredential()
  .description("A production credential")
  .type("PARTNER");
NewProjectEnvironment production = new NewProjectEnvironment()
  .name("PRODUCTION")
  .credential(credential);
ProjectEnvironment environment = api.createProjectEnvironment(projectId, production);
System.out.printf("Environment created. Name: %s%n", environment.getName());
```

We can then retrieve the project we just updated:


API Reference: `GET /projects/{project_id}`

<br />

* Java

```java
OpenBankingPartnerProject project = (OpenBankingPartnerProject) api.getProject(projectId);
System.out.printf("Project retrieved. UUID: %s%n", project.getId());
```

And finally, delete the project:


API Reference: `DELETE /projects/{project_id}`

<br />

* Java

```java
api.deleteProject(projectId);
System.out.println("Project deleted.");
```

Note: That's it 👏. The API client will take care of signing requests and serializing/deserializing requests and responses.

*** ** * ** ***

## Further Reading {#further-reading}

* [The OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification)
* [OpenAPI Generator Workflow Integrations](https://openapi-generator.tech/docs/integrations)
