AI prompts
base on Java library for the Stripe API. # Stripe Java client library
[](https://mvnrepository.com/artifact/com.stripe/stripe-java)
[](https://stripe.dev/stripe-java)
[](https://github.com/stripe/stripe-java/actions?query=branch%3Amaster)
The official [Stripe][stripe] Java client library.
## Installation
### Requirements
- Java 1.8 or later
### Gradle users
Add this dependency to your project's build file:
```groovy
implementation "com.stripe:stripe-java:29.2.0"
```
### Maven users
Add this dependency to your project's POM:
```xml
<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>29.2.0</version>
</dependency>
```
### Others
If you are not using Gradle or Maven, you will need to manually install the following JARs:
1. The Stripe JAR:
- Download the latest release version from [Maven Central](https://repo1.maven.org/maven2/com/stripe/stripe-java/29.0.0/stripe-java-29.0.0.jar)
- Current release version: 29.0.0
2. Google Gson:
- The Stripe JAR builds and tests with Gson version 2.10.1
- Download from [Maven Central](https://repo1.maven.org/maven2/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar)
- We recommend using the same version of Gson if possible to guarantee compatibility, but you should be able to use any stable version of Gson that is 2.10.1 or newer
To use these JARs:
1. Download the JARs from the links provided above
2. Add the JARs to your project's classpath
### [ProGuard][proguard]
If you're planning on using ProGuard, make sure that you exclude the Stripe
client library. You can do this by adding the following to your `proguard.cfg`
file:
```
-keep class com.stripe.** { *; }
```
## Documentation
Please see the [Java API docs][api-docs] for the most
up-to-date documentation.
You can also refer to the [online Javadoc][javadoc].
## Usage
StripeExample.java
```java
import java.util.HashMap;
import java.util.Map;
import com.stripe.StripeClient;
import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
import com.stripe.net.RequestOptions;
import com.stripe.param.CustomerCreateParams;
public class StripeExample {
public static void main(String[] args) {
StripeClient client = new StripeClient("sk_test_...");
CustomerCreateParams params =
CustomerCreateParams
.builder()
.setDescription("Example description")
.setEmail("test@example.com")
.setPaymentMethod("pm_card_visa") // obtained via Stripe.js
.build();
try {
Customer customer = client.customers().create(params);
System.out.println(customer);
} catch (StripeException e) {
e.printStackTrace();
}
}
}
```
See the project's [functional tests][functional-tests] for more examples.
### StripeClient vs legacy pattern
We introduced the `StripeClient` class in v23 of the Java SDK. The legacy pattern used prior to that version is still available to use but will be marked as deprecated soon. Review the [migration guide to use StripeClient](https://github.com/stripe/stripe-java/wiki/Migration-guide-for-v23#stripeclient) to move from the legacy pattern.
Once the legacy pattern is deprecated, new API endpoints will only be accessible in the StripeClient. While there are no current plans to remove the legacy pattern for existing API endpoints, this may change in the future.
### Per-request Configuration
All of the request methods accept an optional `RequestOptions` object. This is
used if you want to set an [idempotency key][idempotency-keys], if you are
using [Stripe Connect][connect-auth], or if you want to pass the secret API
key on each method.
```java
RequestOptions requestOptions = RequestOptions.builder()
.setApiKey("sk_test_...")
.setIdempotencyKey("a1b2c3...")
.setStripeAccount("acct_...")
.build();
client.customers().list(requestOptions);
client.customers().retrieve("cus_123456789", requestOptions);
```
### Configuring automatic retries
The library can be configured to automatically retry requests that fail due to
an intermittent network problem or other knowingly non-deterministic errors.
This can be enabled globally:
```java
StripeClient client = StripeClient.builder()
.setMaxNetworkRetries(2)
.build();
```
Or on a finer grain level using `RequestOptions`:
```java
RequestOptions options = RequestOptions.builder()
.setMaxNetworkRetries(2)
.build();
client.customers().create(params, options);
```
[Idempotency keys][idempotency-keys] are added to requests to guarantee that
retries are safe.
### Configuring Timeouts
Connect and read timeouts can be configured globally:
```java
StripeClient client = StripeClient.builder()
.setConnectTimeout(30 * 1000); // in milliseconds
.setReadTimeout(80 * 1000);
.build();
```
Or on a finer grain level using `RequestOptions`:
```java
RequestOptions options = RequestOptions.builder()
.setConnectTimeout(30 * 1000) // in milliseconds
.setReadTimeout(80 * 1000)
.build();
client.customers().create(params, options);
```
Please take care to set conservative read timeouts. Some API requests can take
some time, and a short timeout increases the likelihood of a problem within our
servers.
### Configuring DNS Cache TTL
We cannot guarantee that the IP address of the Stripe API will be static.
Commonly, default JVM configurations can have their DNS cache TTL set to
forever. If Stripe's IP address changes, your application's requests to
Stripe will all fail until the JVM restarts. Therefore we recommend that
you modify the JVM's [networkaddress.cache.ttl
property](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/doc-files/net-properties.html)
to `60` seconds.
### How to use undocumented parameters and properties
stripe-java is a typed library and it supports all public properties or parameters.
Stripe sometimes has beta which introduces new properties or parameters that are not immediately public. The library does not support these properties or parameters until they are public but there is still an approach that allows you to use them.
#### Parameters
To pass undocumented parameters to Stripe using stripe-java you need to use the `putExtraParam()` method, as shown below:
```java
CustomerCreateParams params =
CustomerCreateParams.builder()
.setEmail("jenny.rosen@example.com")
.putExtraParam("secret_feature_enabled", "true")
.putExtraParam("secret_parameter[primary]", "primary value")
.putExtraParam("secret_parameter[secondary]", "secondary value")
.build();
client.customers().create(params);
```
#### Properties
To retrieve undocumented properties from Stripe using Java you can use an option in the library to return the raw JSON object and return the property as a native type. An example of this is shown below:
```java
final Customer customer = client.customers().retrieve("cus_1234");
Boolean featureEnabled =
customer.getRawJsonObject()
.getAsJsonPrimitive("secret_feature_enabled")
.getAsBoolean();
String primaryValue =
customer.getRawJsonObject()
.getAsJsonObject("secret_parameter")
.getAsJsonPrimitive("primary")
.getAsString();
String secondaryValue =
customer.getRawJsonObject()
.getAsJsonObject("secret_parameter")
.getAsJsonPrimitive("secondary")
.getAsString();
```
### Writing a plugin
If you're writing a plugin that uses the library, we'd appreciate it if you
identified using `Stripe.setAppInfo()`:
```java
Stripe.setAppInfo("MyAwesomePlugin", "1.2.34", "https://myawesomeplugin.info");
```
This information is passed along when the library makes calls to the Stripe
API.
### Request latency telemetry
By default, the library sends request latency telemetry to Stripe. These
numbers help Stripe improve the overall latency of its API for all users.
You can disable this behavior if you prefer:
```java
Stripe.enableTelemetry = false;
```
### Public Preview SDKs
Stripe has features in the [public preview phase](https://docs.stripe.com/release-phases) that can be accessed via versions of this package that have the `-beta.X` suffix like `25.2.0-beta.2`.
We would love for you to try these as we incrementally release new features and improve them based on your feedback.
To install, choose the version that includes support for the preview feature you are interested in by reviewing the [releases page](https://github.com/stripe/stripe-java/releases/) and then use it [installation steps above](#installation).
> **Note**
> There can be breaking changes between two versions of the public preview SDKs without a bump in the major version. Therefore we recommend pinning the package version to a specific version. This way you can install the same version each time without breaking changes unless you are intentionally looking for the latest public preview SDK.
Some preview features require a name and version to be set in the `Stripe-Version` header like `feature_beta=v3`. If your preview feature has this requirement, use the `Stripe.addBetaVersion` function (available only in the public preview SDKs):
```java
Stripe.addBetaVersion("feature_beta", "v3");
```
### Custom requests
If you would like to send a request to an undocumented API (for example you are in a private beta), or if you prefer to bypass the method definitions in the library and specify your request details directly, you can use the `rawRequest` method on `StripeClient`.
```java
// (Optional) Create a RawRequestOptions object, allowing you to set per-request
// configuration options like additional headers.
Map<String, String> stripeVersionHeader = new HashMap<>();
stripeVersionHeader.put("Stripe-Version", "2024-09-30.acacia");
RawRequestOptions options = RawRequestOptions.builder()
.setAdditionalHeaders(stripeVersionHeader)
.build();
// Make the request using the StripeClient.rawRequest() method.
StripeClient client = new StripeClient("sk_test_...");
final StripeResponse response =
client.rawRequest(
ApiResource.RequestMethod.POST, "/v1/customers", "name=johndoe&email=johndoe@example.com", options);
// (Optional) response.body() is a string. You can call
// StripeClient.deserialize() to get a StripeObject
// Pass ApiMode.V2 if the endpoint you are targeting starts with "/v2", else pass ApiMode.V1
StripeObject object = client.deserialize(response.body(), ApiMode.V1);
// or cast it if a corresponding response class exists in the SDK
Customer customer = (Customer) client.deserialize(response.body(), ApiMode.V1);
```
## Support
New features and bug fixes are released on the latest major version of the Stripe Java client library. If you are on an older major version, we recommend that you upgrade to the latest in order to use the new features and bug fixes including those for security vulnerabilities. Older major versions of the package will continue to be available for use, but will not be receiving any updates.
## Development
[Contribution guidelines for this project](CONTRIBUTING.md)
JDK 17 is required to build the Stripe Java library. By default, tests use the same Java runtime as the build.
To use a custom version of Java runtime for tests set the `JAVA_TEST_HOME` environment variable to runtime's
home directory.
The test suite depends on [stripe-mock], so make sure to fetch and run it from a
background terminal ([stripe-mock's README][stripe-mock] also contains
instructions for installing via Homebrew and other methods):
```sh
go get -u github.com/stripe/stripe-mock
stripe-mock
```
We use [just](https://github.com/casey/just) for conveniently running development tasks. You can use them directly, or copy the commands out of the `justfile`. To our help docs, run `just`.
To run all checks (tests and code formatting):
```sh
./gradlew check
```
To run the tests:
```sh
just test
# or: ./gradlew test
```
You can run particular tests by passing `--tests Class#method`. Make sure you
use the fully qualified class name. For example:
```sh
just test-one com.stripe.model.AccountTest
just test-one com.stripe.functional.CustomerTest
just test-one com.stripe.functional.CustomerTest.testCustomerCreate
# or: ./gradlew test --tests com.stripe.model.AccountTest
# or: ./gradlew test --tests com.stripe.functional.CustomerTest
# or: ./gradlew test --tests com.stripe.functional.CustomerTest.testCustomerCreate
```
The library uses [Spotless][spotless] along with
[google-java-format][google-java-format] for code formatting. Code must be
formatted before PRs are submitted, otherwise CI will fail. Run the formatter
with:
```sh
just format
# or: ./gradlew spotlessApply
```
The library uses [Project Lombok][lombok]. While it is not a requirement, you
might want to install a [plugin][lombok-plugins] for your favorite IDE to
facilitate development.
[api-docs]: https://stripe.com/docs/api?lang=java
[connect-auth]: https://stripe.com/docs/connect/authentication#stripe-account-header
[functional-tests]: https://github.com/stripe/stripe-java/blob/master/src/test/java/com/stripe/functional/
[google-java-format]: https://github.com/google/google-java-format
[gson]: https://github.com/google/gson
[idempotency-keys]: https://stripe.com/docs/api/idempotent_requests?lang=java
[javadoc]: https://stripe.dev/stripe-java
[lombok]: https://projectlombok.org
[lombok-plugins]: https://projectlombok.org/setup/overview
[proguard]: https://www.guardsquare.com/en/products/proguard
[spotless]: https://github.com/diffplug/spotless
[stripe]: https://stripe.com
[stripe-mock]: https://github.com/stripe/stripe-mock
<!--
# vim: set tw=79:
-->
", Assign "at most 3 tags" to the expected json: {"id":"2756","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"