Class: WhopSDK::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/whop_sdk/client.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

60.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

8.0

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(api_key: ENV["WHOP_API_KEY"], webhook_key: ENV["WHOP_WEBHOOK_SECRET"], app_id: ENV["WHOP_APP_ID"], user_token_public_key: ENV["WHOP_USER_TOKEN_PUBLIC_KEY"], user_token_jwks_url: ENV["WHOP_USER_TOKEN_JWKS_URL"], base_url: ENV["WHOP_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

must prepend your key/token with the word ‘Bearer’, which will look like ‘Bearer ***************************` Defaults to `ENV`

tokens Defaults to ‘ENV`

user tokens. When set, #verify_user_token skips remote JWKS fetching. Defaults to ‘ENV`

Defaults to ‘ENV`, then to the canonical Whop endpoint.

‘“api.example.com/v2/”`. Defaults to `ENV`

Parameters:

  • api_key (String, nil) (defaults to: ENV["WHOP_API_KEY"])

    A company API key, company scoped JWT, app API key, or user OAuth token. You

  • webhook_key (String, nil) (defaults to: ENV["WHOP_WEBHOOK_SECRET"])

    Defaults to ‘ENV`

  • app_id (String, nil) (defaults to: ENV["WHOP_APP_ID"])

    When using the SDK in app mode pass this parameter to allow verifying user

  • user_token_public_key (String, nil) (defaults to: ENV["WHOP_USER_TOKEN_PUBLIC_KEY"])

    Static public key (PEM or JWK JSON) used to verify

  • user_token_jwks_url (String, nil) (defaults to: ENV["WHOP_USER_TOKEN_JWKS_URL"])

    Override the JWKS URL used by #verify_user_token.

  • base_url (String, nil) (defaults to: ENV["WHOP_BASE_URL"])

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/whop_sdk/client.rb', line 293

def initialize(
  api_key: ENV["WHOP_API_KEY"],
  webhook_key: ENV["WHOP_WEBHOOK_SECRET"],
  app_id: ENV["WHOP_APP_ID"],
  user_token_public_key: ENV["WHOP_USER_TOKEN_PUBLIC_KEY"],
  user_token_jwks_url: ENV["WHOP_USER_TOKEN_JWKS_URL"],
  base_url: ENV["WHOP_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  base_url ||= "https://api.whop.com/api/v1"

  if api_key.nil?
    raise ArgumentError.new("api_key is required, and can be set via environ: \"WHOP_API_KEY\"")
  end

  headers = {
    "x-whop-app-id" => (@app_id = app_id&.to_s)
  }

  @api_key = api_key.to_s
  @webhook_key = webhook_key&.to_s
  @user_token_public_key = user_token_public_key&.to_s
  @user_token_jwks_url = user_token_jwks_url&.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    headers: headers
  )

  @apps = WhopSDK::Resources::Apps.new(client: self)
  @invoices = WhopSDK::Resources::Invoices.new(client: self)
  @course_lesson_interactions = WhopSDK::Resources::CourseLessonInteractions.new(client: self)
  @products = WhopSDK::Resources::Products.new(client: self)
  @companies = WhopSDK::Resources::Companies.new(client: self)
  @webhooks = WhopSDK::Resources::Webhooks.new(client: self)
  @plans = WhopSDK::Resources::Plans.new(client: self)
  @entries = WhopSDK::Resources::Entries.new(client: self)
  @forum_posts = WhopSDK::Resources::ForumPosts.new(client: self)
  @transfers = WhopSDK::Resources::Transfers.new(client: self)
  @ledger_accounts = WhopSDK::Resources::LedgerAccounts.new(client: self)
  @memberships = WhopSDK::Resources::Memberships.new(client: self)
  @authorized_users = WhopSDK::Resources::AuthorizedUsers.new(client: self)
  @app_builds = WhopSDK::Resources::AppBuilds.new(client: self)
  @shipments = WhopSDK::Resources::Shipments.new(client: self)
  @checkout_configurations = WhopSDK::Resources::CheckoutConfigurations.new(client: self)
  @messages = WhopSDK::Resources::Messages.new(client: self)
  @chat_channels = WhopSDK::Resources::ChatChannels.new(client: self)
  @users = WhopSDK::Resources::Users.new(client: self)
  @payments = WhopSDK::Resources::Payments.new(client: self)
  @support_channels = WhopSDK::Resources::SupportChannels.new(client: self)
  @experiences = WhopSDK::Resources::Experiences.new(client: self)
  @reactions = WhopSDK::Resources::Reactions.new(client: self)
  @members = WhopSDK::Resources::Members.new(client: self)
  @forums = WhopSDK::Resources::Forums.new(client: self)
  @promo_codes = WhopSDK::Resources::PromoCodes.new(client: self)
  @courses = WhopSDK::Resources::Courses.new(client: self)
  @course_chapters = WhopSDK::Resources::CourseChapters.new(client: self)
  @course_lessons = WhopSDK::Resources::CourseLessons.new(client: self)
  @reviews = WhopSDK::Resources::Reviews.new(client: self)
  @course_students = WhopSDK::Resources::CourseStudents.new(client: self)
  @access_tokens = WhopSDK::Resources::AccessTokens.new(client: self)
  @notifications = WhopSDK::Resources::Notifications.new(client: self)
  @disputes = WhopSDK::Resources::Disputes.new(client: self)
  @refunds = WhopSDK::Resources::Refunds.new(client: self)
  @withdrawals = WhopSDK::Resources::Withdrawals.new(client: self)
  @account_links = WhopSDK::Resources::AccountLinks.new(client: self)
  @setup_intents = WhopSDK::Resources::SetupIntents.new(client: self)
  @payment_methods = WhopSDK::Resources::PaymentMethods.new(client: self)
  @fee_markups = WhopSDK::Resources::FeeMarkups.new(client: self)
  @payout_methods = WhopSDK::Resources::PayoutMethods.new(client: self)
  @verifications = WhopSDK::Resources::Verifications.new(client: self)
  @leads = WhopSDK::Resources::Leads.new(client: self)
  @topups = WhopSDK::Resources::Topups.new(client: self)
  @files = WhopSDK::Resources::Files.new(client: self)
  @company_token_transactions = WhopSDK::Resources::CompanyTokenTransactions.new(client: self)
  @dm_members = WhopSDK::Resources::DmMembers.new(client: self)
  @ai_chats = WhopSDK::Resources::AIChats.new(client: self)
  @dm_channels = WhopSDK::Resources::DmChannels.new(client: self)
  @dispute_alerts = WhopSDK::Resources::DisputeAlerts.new(client: self)
  @resolution_center_cases = WhopSDK::Resources::ResolutionCenterCases.new(client: self)
  @payout_accounts = WhopSDK::Resources::PayoutAccounts.new(client: self)
  @affiliates = WhopSDK::Resources::Affiliates.new(client: self)
end

Instance Attribute Details

#access_tokensWhopSDK::Resources::AccessTokens (readonly)

Access tokens



170
171
172
# File 'lib/whop_sdk/client.rb', line 170

def access_tokens
  @access_tokens
end

Account links



190
191
192
# File 'lib/whop_sdk/client.rb', line 190

def 
  @account_links
end

#affiliatesWhopSDK::Resources::Affiliates (readonly)

Affiliates



254
255
256
# File 'lib/whop_sdk/client.rb', line 254

def affiliates
  @affiliates
end

#ai_chatsWhopSDK::Resources::AIChats (readonly)

Ai chats



234
235
236
# File 'lib/whop_sdk/client.rb', line 234

def ai_chats
  @ai_chats
end

#api_keyString (readonly)

A company API key, company scoped JWT, app API key, or user OAuth token. You must prepend your key/token with the word ‘Bearer’, which will look like ‘Bearer ***************************`

Returns:

  • (String)


22
23
24
# File 'lib/whop_sdk/client.rb', line 22

def api_key
  @api_key
end

#app_buildsWhopSDK::Resources::AppBuilds (readonly)

App builds



98
99
100
# File 'lib/whop_sdk/client.rb', line 98

def app_builds
  @app_builds
end

#app_idString? (readonly)

When using the SDK in app mode pass this parameter to allow verifying user tokens

Returns:

  • (String, nil)


30
31
32
# File 'lib/whop_sdk/client.rb', line 30

def app_id
  @app_id
end

#appsWhopSDK::Resources::Apps (readonly)

Apps



46
47
48
# File 'lib/whop_sdk/client.rb', line 46

def apps
  @apps
end

#authorized_usersWhopSDK::Resources::AuthorizedUsers (readonly)

Authorized users



94
95
96
# File 'lib/whop_sdk/client.rb', line 94

def authorized_users
  @authorized_users
end

#chat_channelsWhopSDK::Resources::ChatChannels (readonly)

Chat channels



114
115
116
# File 'lib/whop_sdk/client.rb', line 114

def chat_channels
  @chat_channels
end

#checkout_configurationsWhopSDK::Resources::CheckoutConfigurations (readonly)

Checkout configurations



106
107
108
# File 'lib/whop_sdk/client.rb', line 106

def checkout_configurations
  @checkout_configurations
end

#companiesWhopSDK::Resources::Companies (readonly)

Companies



62
63
64
# File 'lib/whop_sdk/client.rb', line 62

def companies
  @companies
end

#company_token_transactionsWhopSDK::Resources::CompanyTokenTransactions (readonly)

Company token transactions



226
227
228
# File 'lib/whop_sdk/client.rb', line 226

def company_token_transactions
  @company_token_transactions
end

#course_chaptersWhopSDK::Resources::CourseChapters (readonly)

Course chapters



154
155
156
# File 'lib/whop_sdk/client.rb', line 154

def course_chapters
  @course_chapters
end

#course_lesson_interactionsWhopSDK::Resources::CourseLessonInteractions (readonly)

Course lesson interactions



54
55
56
# File 'lib/whop_sdk/client.rb', line 54

def course_lesson_interactions
  @course_lesson_interactions
end

#course_lessonsWhopSDK::Resources::CourseLessons (readonly)

Course lessons



158
159
160
# File 'lib/whop_sdk/client.rb', line 158

def course_lessons
  @course_lessons
end

#course_studentsWhopSDK::Resources::CourseStudents (readonly)

Course students



166
167
168
# File 'lib/whop_sdk/client.rb', line 166

def course_students
  @course_students
end

#coursesWhopSDK::Resources::Courses (readonly)

Courses



150
151
152
# File 'lib/whop_sdk/client.rb', line 150

def courses
  @courses
end

#dispute_alertsWhopSDK::Resources::DisputeAlerts (readonly)

Dispute alerts



242
243
244
# File 'lib/whop_sdk/client.rb', line 242

def dispute_alerts
  @dispute_alerts
end

#disputesWhopSDK::Resources::Disputes (readonly)

Disputes



178
179
180
# File 'lib/whop_sdk/client.rb', line 178

def disputes
  @disputes
end

#dm_channelsWhopSDK::Resources::DmChannels (readonly)

Dm channels



238
239
240
# File 'lib/whop_sdk/client.rb', line 238

def dm_channels
  @dm_channels
end

#dm_membersWhopSDK::Resources::DmMembers (readonly)

Dm members



230
231
232
# File 'lib/whop_sdk/client.rb', line 230

def dm_members
  @dm_members
end

#entriesWhopSDK::Resources::Entries (readonly)

Entries



74
75
76
# File 'lib/whop_sdk/client.rb', line 74

def entries
  @entries
end

#experiencesWhopSDK::Resources::Experiences (readonly)

Experiences



130
131
132
# File 'lib/whop_sdk/client.rb', line 130

def experiences
  @experiences
end

#fee_markupsWhopSDK::Resources::FeeMarkups (readonly)

Fee markups



202
203
204
# File 'lib/whop_sdk/client.rb', line 202

def fee_markups
  @fee_markups
end

#filesWhopSDK::Resources::Files (readonly)

Files



222
223
224
# File 'lib/whop_sdk/client.rb', line 222

def files
  @files
end

#forum_postsWhopSDK::Resources::ForumPosts (readonly)

Forum posts



78
79
80
# File 'lib/whop_sdk/client.rb', line 78

def forum_posts
  @forum_posts
end

#forumsWhopSDK::Resources::Forums (readonly)

Forums



142
143
144
# File 'lib/whop_sdk/client.rb', line 142

def forums
  @forums
end

#invoicesWhopSDK::Resources::Invoices (readonly)

Invoices



50
51
52
# File 'lib/whop_sdk/client.rb', line 50

def invoices
  @invoices
end

#leadsWhopSDK::Resources::Leads (readonly)

Leads



214
215
216
# File 'lib/whop_sdk/client.rb', line 214

def leads
  @leads
end

#ledger_accountsWhopSDK::Resources::LedgerAccounts (readonly)

Ledger accounts



86
87
88
# File 'lib/whop_sdk/client.rb', line 86

def ledger_accounts
  @ledger_accounts
end

#membersWhopSDK::Resources::Members (readonly)

Members



138
139
140
# File 'lib/whop_sdk/client.rb', line 138

def members
  @members
end

#membershipsWhopSDK::Resources::Memberships (readonly)

Memberships



90
91
92
# File 'lib/whop_sdk/client.rb', line 90

def memberships
  @memberships
end

#messagesWhopSDK::Resources::Messages (readonly)

Messages



110
111
112
# File 'lib/whop_sdk/client.rb', line 110

def messages
  @messages
end

#notificationsWhopSDK::Resources::Notifications (readonly)

Notifications



174
175
176
# File 'lib/whop_sdk/client.rb', line 174

def notifications
  @notifications
end

#payment_methodsWhopSDK::Resources::PaymentMethods (readonly)

Payment methods



198
199
200
# File 'lib/whop_sdk/client.rb', line 198

def payment_methods
  @payment_methods
end

#paymentsWhopSDK::Resources::Payments (readonly)

Payments



122
123
124
# File 'lib/whop_sdk/client.rb', line 122

def payments
  @payments
end

#payout_accountsWhopSDK::Resources::PayoutAccounts (readonly)

Payout accounts



250
251
252
# File 'lib/whop_sdk/client.rb', line 250

def payout_accounts
  @payout_accounts
end

#payout_methodsWhopSDK::Resources::PayoutMethods (readonly)

Payout methods



206
207
208
# File 'lib/whop_sdk/client.rb', line 206

def payout_methods
  @payout_methods
end

#plansWhopSDK::Resources::Plans (readonly)

Plans



70
71
72
# File 'lib/whop_sdk/client.rb', line 70

def plans
  @plans
end

#productsWhopSDK::Resources::Products (readonly)

Products



58
59
60
# File 'lib/whop_sdk/client.rb', line 58

def products
  @products
end

#promo_codesWhopSDK::Resources::PromoCodes (readonly)

Promo codes



146
147
148
# File 'lib/whop_sdk/client.rb', line 146

def promo_codes
  @promo_codes
end

#reactionsWhopSDK::Resources::Reactions (readonly)

Reactions



134
135
136
# File 'lib/whop_sdk/client.rb', line 134

def reactions
  @reactions
end

#refundsWhopSDK::Resources::Refunds (readonly)

Refunds



182
183
184
# File 'lib/whop_sdk/client.rb', line 182

def refunds
  @refunds
end

#resolution_center_casesWhopSDK::Resources::ResolutionCenterCases (readonly)

Resolution center cases



246
247
248
# File 'lib/whop_sdk/client.rb', line 246

def resolution_center_cases
  @resolution_center_cases
end

#reviewsWhopSDK::Resources::Reviews (readonly)

Reviews



162
163
164
# File 'lib/whop_sdk/client.rb', line 162

def reviews
  @reviews
end

#setup_intentsWhopSDK::Resources::SetupIntents (readonly)

Setup intents



194
195
196
# File 'lib/whop_sdk/client.rb', line 194

def setup_intents
  @setup_intents
end

#shipmentsWhopSDK::Resources::Shipments (readonly)

Shipments



102
103
104
# File 'lib/whop_sdk/client.rb', line 102

def shipments
  @shipments
end

#support_channelsWhopSDK::Resources::SupportChannels (readonly)

Support channels



126
127
128
# File 'lib/whop_sdk/client.rb', line 126

def support_channels
  @support_channels
end

#topupsWhopSDK::Resources::Topups (readonly)

Topups



218
219
220
# File 'lib/whop_sdk/client.rb', line 218

def topups
  @topups
end

#transfersWhopSDK::Resources::Transfers (readonly)

Transfers



82
83
84
# File 'lib/whop_sdk/client.rb', line 82

def transfers
  @transfers
end

#user_token_jwks_urlString? (readonly)

Override for the JWKS endpoint used by #verify_user_token. Defaults to the canonical Whop endpoint when unset.

Returns:

  • (String, nil)


42
43
44
# File 'lib/whop_sdk/client.rb', line 42

def user_token_jwks_url
  @user_token_jwks_url
end

#user_token_public_keyString? (readonly)

Static public key (PEM or JWK JSON) used by #verify_user_token to verify user tokens. When set, the SDK skips remote JWKS fetching. Prefer #user_token_jwks_url (or the default) so key rotation is handled automatically.

Returns:

  • (String, nil)


37
38
39
# File 'lib/whop_sdk/client.rb', line 37

def user_token_public_key
  @user_token_public_key
end

#usersWhopSDK::Resources::Users (readonly)

Users



118
119
120
# File 'lib/whop_sdk/client.rb', line 118

def users
  @users
end

#verificationsWhopSDK::Resources::Verifications (readonly)

Verifications



210
211
212
# File 'lib/whop_sdk/client.rb', line 210

def verifications
  @verifications
end

#webhook_keyString? (readonly)

Returns:

  • (String, nil)


25
26
27
# File 'lib/whop_sdk/client.rb', line 25

def webhook_key
  @webhook_key
end

#webhooksWhopSDK::Resources::Webhooks (readonly)

Webhooks



66
67
68
# File 'lib/whop_sdk/client.rb', line 66

def webhooks
  @webhooks
end

#withdrawalsWhopSDK::Resources::Withdrawals (readonly)

Withdrawals



186
187
188
# File 'lib/whop_sdk/client.rb', line 186

def withdrawals
  @withdrawals
end

Instance Method Details

#verify_user_token(token_or_headers, **opts) ⇒ Helpers::VerifyUserToken::UserTokenPayload?

Verifies a Whop user token. Same signature as #verify_user_token! but returns ‘nil` on any validation failure instead of raising.



409
410
411
412
413
# File 'lib/whop_sdk/client.rb', line 409

def verify_user_token(token_or_headers, **opts)
  verify_user_token!(token_or_headers, **opts)
rescue StandardError
  nil
end

#verify_user_token!(token_or_headers, **opts) ⇒ Helpers::VerifyUserToken::UserTokenPayload

Verifies a Whop user token.

Parameters:

  • token_or_headers (String, Hash, nil)

    The token string or headers hash

  • app_id (String, nil)

    The app id to verify against

  • public_key (String, nil)

    Static public key (PEM or JWK JSON). When set, the SDK skips remote JWKS fetching. Defaults to the client’s ‘user_token_public_key`.

  • jwks_url (String, nil)

    Override the JWKS URL. Defaults to the client’s ‘user_token_jwks_url`, then to the canonical Whop endpoint.

  • header_name (String, nil)

    The header name to read the token from

Returns:

Raises:

  • (StandardError)

    If verification fails



395
396
397
398
399
400
401
402
403
# File 'lib/whop_sdk/client.rb', line 395

def verify_user_token!(token_or_headers, **opts)
  opts[:app_id] ||= app_id
  opts[:public_key] = user_token_public_key if opts[:public_key].nil? && user_token_public_key && !user_token_public_key.empty?
  opts[:jwks_url] = user_token_jwks_url if opts[:jwks_url].nil? && user_token_jwks_url && !user_token_jwks_url.empty?
  unless opts[:app_id]
    raise StandardError, "You must set app_id in the Whop client if you want to verify user tokens"
  end
  Helpers::VerifyUserToken.verify_user_token!(token_or_headers, **opts)
end