Skip to content

Commit 755b804

Browse files
Updated stripeclient snippets in Readme.md (stripe#3137)
* Updated stripeclient snippets in Readme.md * Added section for using stripeclient * Updates * Updated authentication config * Update README.md * Update README.md --------- Co-authored-by: Ramya Rao <[email protected]>
1 parent f792150 commit 755b804

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

README.md

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,31 @@ how to use the library.
4343

4444
## Usage
4545

46-
### Authentication
46+
### Using StripeClient
4747

48-
Stripe authenticates API requests using your account’s secret key, which you can find in the Stripe Dashboard. By default, secret keys can be used to perform any API request without restriction.
48+
In version 46 of the Stripe .NET SDK, we have enhanced the `StripeClient` class to be the entry point to access all services that had to be previously independently instantiated with global configuration. This improves discoverability during IDE auto-completion and creates a more intuitive developer experience for you.
4949

50-
Use `StripeConfiguration.ApiKey` property to set the secret key.
50+
Each client instantiation can have its own configuration so you can access Stripe API with different API keys or different configuration (like number of retries) on a per client basis and without changing a global configuration.
5151

5252
```C#
53+
// StripeClient pattern (Recommended)
54+
var client = new StripeClient("sk_test_...");
55+
Customer customer = client.V1.Customers.Get("cus_1234");
56+
57+
// Global Configuration pattern (Legacy)
5358
StripeConfiguration.ApiKey = "sk_test_...";
59+
var service = new CustomerService();
60+
Customer customer = service.Get("cus_1234");
61+
```
62+
63+
The previous global configuration pattern will continue to be supported.
64+
65+
### Authentication
66+
67+
Stripe authenticates API requests using your account’s secret key, which you can find in the Stripe Dashboard. By default, secret keys can be used to perform any API request without restriction.
68+
69+
```C#
70+
var client = new StripeClient("sk_test_...");
5471
```
5572

5673
### Creating a resource
@@ -63,8 +80,8 @@ var options = new CustomerCreateOptions
6380
6481
};
6582

66-
var service = new CustomerService();
67-
Customer customer = service.Create(options);
83+
var client = new StripeClient("sk_test_...");
84+
Customer customer = client.V1.Customers.Create(options);
6885

6986
// Newly created customer is returned
7087
Console.WriteLine(customer.Email);
@@ -75,8 +92,8 @@ Console.WriteLine(customer.Email);
7592
The `Retrieve` method of the service class can be used to retrieve a resource:
7693

7794
```C#
78-
var service = new CustomerService();
79-
Customer customer = service.Get("cus_1234");
95+
var client = new StripeClient("sk_test_...");
96+
Customer customer = client.V1.Customers.Get("cus_1234");
8097

8198
Console.WriteLine(customer.Email);
8299
```
@@ -91,8 +108,8 @@ var options = new CustomerUpdateOptions
91108
92109
};
93110

94-
var service = new CustomerService();
95-
Customer customer = service.Update("cus_123", options);
111+
var client = new StripeClient("sk_test_...");
112+
Customer customer = client.V1.Customers.Update("cus_123", options);
96113

97114
// The updated customer is returned
98115
Console.WriteLine(customer.Email);
@@ -103,8 +120,8 @@ Console.WriteLine(customer.Email);
103120
The `Delete` method of the service class can be used to delete a resource:
104121

105122
```C#
106-
var service = new CustomerService();
107-
Customer customer = service.Delete("cus_123", options);
123+
var client = new StripeClient("sk_test_...");
124+
Customer customer = client.V1.Customers.Delete("cus_123", options);
108125
```
109126

110127
### Listing a resource
@@ -115,8 +132,8 @@ The `List` method on the service class can be used to list resources page-by-pag
115132
> The `List` method returns only a single page, you have to manually continue the iteration using the `StartingAfter` parameter.
116133
117134
```C#
118-
var service = new CustomerService();
119-
var customers = service.List();
135+
var client = new StripeClient("sk_test_...");
136+
var customers = client.V1.Customers.List();
120137

121138
string lastId = null;
122139

@@ -145,8 +162,8 @@ foreach (Customer customer in customers)
145162
The `ListAutoPaging` method on the service class can be used to automatically iterate over all pages.
146163

147164
```C#
148-
var service = new CustomerService();
149-
var customers = service.ListAutoPaging();
165+
var client = new StripeClient("sk_test_...");
166+
var customers = client.V1.Customers.ListAutoPaging();
150167

151168
// Enumerate all pages of the list
152169
foreach (Customer customer in customers)
@@ -157,7 +174,7 @@ foreach (Customer customer in customers)
157174

158175
### Per-request configuration
159176

160-
All of the service methods accept an optional `RequestOptions` object. This is
177+
All the service methods accept an optional `RequestOptions` object. This is
161178
used if you want to set an [idempotency key][idempotency-keys], if you are
162179
using [Stripe Connect][connect-auth], or if you want to pass the secret API
163180
key on each method.
@@ -216,22 +233,21 @@ options.AddExtraParam("secret_feature_enabled", "true");
216233
options.AddExtraParam("secret_parameter[primary]", "primary value");
217234
options.AddExtraParam("secret_parameter[secondary]", "secondary value");
218235

219-
var service = new CustomerService();
220-
var customer = service.Create(options);
236+
var client = new StripeClient("sk_test_...");
237+
var customer = client.V1.Customers.Create(options);
221238
```
222239

223240
#### Properties
224241

225242
To retrieve undocumented properties from Stripe using C# you can use an option in the library to return the raw JSON object and return the property. An example of this is shown below:
226243

227244
```c#
228-
var service = new CustomerService();
229-
var customer = service.Get("cus_1234");
245+
var client = new StripeClient("sk_test_...");
246+
var customer = client.V1.Customers.Get("cus_1234");
230247

231248
customer.RawJObject["secret_feature_enabled"];
232249
customer.RawJObject["secret_parameter"]["primary"];
233250
customer.RawJObject["secret_parameter"]["secondary"];
234-
235251
```
236252

237253
### Writing a plugin

0 commit comments

Comments
 (0)