# Hosted Fields JavaScript Reference Building with an AI agent? Start at https://docs.payme.io/agents.md Last updated: 2026-11-21 ## Related - [Build your own checkout form with Hosted Fields](/accepting-payments/checkout/hosted-fields-jsapi-guide) - [Charge a Hosted Fields token](/payments/hosted-fields/post-generate-sale-hosted-fields-jsapi) For the whole flow, see [Build your own checkout form with Hosted Fields](/accepting-payments/checkout/hosted-fields-jsapi-guide). ## PayMe.create options `PayMe.create(publicKey, options)` resolves to an instance. `options` is optional. An unknown option, or a value of the wrong type, makes it reject. | Option | Type | Default | What it does | |---|---|---|---| | `testMode` | boolean | `false` | `true` processes in the sandbox, `false` in production. It must match the key and the server that charges the token. | | `language` | `'en'` \| `'he'` | `'en'` | `'he'` makes the fields right-to-left and returns PayMe's error messages in Hebrew. Field messages stay in English unless you pass `messages` (see the `compact-hebrew` example). | | `tokenIsPermanent` | boolean | `true` | `true`: the token can be charged again. `false`: it charges once; a second charge fails with `604` "Buyer inactive". | | `buyerIsPrivate` | boolean | `false` | `true`: only this seller can charge the token. `false`: any seller of the same partner can. | | `buyerPerformValidation` | boolean | `true` | Validates the card when the token is created. | | `domainName` | string | the page's host | Apple Pay only. The top-level domain, e.g. `shop.example.com`, when the SDK runs inside another site's iframe. A bare domain, without `https://`. | `PayMe.version` holds the SDK's version, e.g. `1.21.0`. ## Fields `instance.hostedFields()` returns a manager. `fields.create(name, options)` makes a field; calling it again for the same name returns the same field and ignores the new options. `field.mount(selector)` shows the field in the first element the selector finds. It returns a promise, rejects with ` already mounted` if the field is already mounted, and throws `Container for not found` at once if no element matches. Create only the fields you mount: `tokenize()` waits for a value from every created field. | `PayMe.fields` | Name | Required for `tokenize` | |---|---|---| | `NUMBER` | `cardNumber` | Yes, always a mounted field | | `EXPIRATION` | `cardExpiration` | Yes, always a mounted field | | `CVC` | `cvc` | Yes, always a mounted field | | `NAME_FIRST` | `payerFirstName` | Yes: mount it or pass its value | | `NAME_LAST` | `payerLastName` | Yes: mount it or pass its value | | `EMAIL` | `payerEmail` | Yes: mount it or pass its value | | `PHONE` | `payerPhone` | Yes: mount it or pass its value | | `SOCIAL_ID` | `payerSocialId` | No | | `ZIP_CODE` | `payerZipCode` | No | The card number, expiry and CVV can't be passed as values, so those three fields are always mounted. A payer detail you don't mount goes in the `tokenize` data instead. **Field methods:** | Method | What it does | |---|---| | `field.on(event, handler)` | Listens to an event (see [Field events](#field-events)) | | `field.once(event, handler)` | Listens to the next occurrence only | | `field.removeListener(event, handler)` | Stops one listener | | `field.removeAllListeners(event?)` | Stops every listener, or every listener of one event | | `field.focus()` / `field.blur()` | Moves the focus into or out of the field | | `field.getState()` | Returns `{ field, isValid, isEmpty, message }` | `fields.set({ colorScheme: PayMe.colorScheme.Dark })` sets the color scheme of every field: `PayMe.colorScheme.Dark`, `Light` or `Normal`. ## Field options ```js const cardNumber = fields.create(PayMe.fields.NUMBER, { placeholder: 'Card number', messages: { required: 'Enter your card number', invalid: 'Check your card number' }, styles: { base: { color: '#000000', '::placeholder': { color: '#8a8a8a' } }, invalid: { color: 'red' }, valid: { color: 'green' }, }, }); ``` `placeholder`, `messages` and `styles` are the only options. Any other key throws. ## Field styling Because a PayMe field is protected, `styles` takes three groups, `base`, `invalid` and `valid`, with only these properties. Anything else, including `font-family`, is dropped. - `color` - `font-size` - `text-align` - `letter-spacing` - `text-decoration` - `text-shadow` - `text-transform` - `::placeholder`, nested inside a group Everything around the field is yours: style its container for borders, backgrounds and focus. Your own inputs for payer details take any CSS. ## Field events `field.on(event, handler)`: | Event | Fields | When | |---|---|---| | `change` | All | Like the standard event | | `blur` | All | Like the standard event | | `focus` | All | Like the standard event | | `keyup` | All | Like the standard event | | `keydown` | All | Like the standard event | | `keypress` | All | Like the standard event | | `validity-changed` | All | The field's validity changed: use it to show messages | | `card-type-changed` | `cardNumber` | The card's brand was detected: use it to show an icon | The handler gets no card data: ```js { type: 'validity-changed', event: 'validity-changed', field: 'cardNumber', isValid: false, isEmpty: false, message: 'Invalid credit card number', // when invalid cardType: 'mastercard', // on card-type-changed } ``` The field's messages are in English unless you pass your own `messages`, whatever the `language` option. | `cardType` | Brand | Starts with | |---|---|---| | `visa` | Visa | 4 (except 4175, 4571) | | `mastercard` | Mastercard | 51–55, 2221–2299, 23–27 | | `amex` | American Express | 34, 37 | | `diners` | Diners Club | 300–305, 309, 36, 38, 39 | | `discover` | Discover | 6011, 644–649, 65 | | `jcb` | JCB | 35 | | `jcb15` | JCB (15 digits) | 2131, 1800 | | `maestro` | Maestro | 50, 56–58, 6304, 67 | | `unionPay` | UnionPay | 62, 81 | | `mir` | Mir | 2200–2204 | | `dankort` | Dankort | 5019, 4175, 4571 | | `instapayment` | InstaPayment | 637–639 | | `uatp` | UATP | 1 (except 1800) | | `unknown` | Not detected | Anything else, including local Isracard cards (8–10 digits), which the number field accepts | ## Tokenize `instance.tokenize(saleData)` returns a promise. `saleData` takes only these keys. Any other key, such as `fullName` or `installments`, makes it reject. | Key | Required | Value | |---|---|---| | `payerFirstName` | Yes, unless mounted | Any text | | `payerLastName` | Yes, unless mounted | Any text | | `payerEmail` | Yes, unless mounted | An email address | | `payerPhone` | Yes, unless mounted | Digits only, with an optional `+` and country code, e.g. `0501234567` or `+972501234567`. Spaces and dashes fail with "Invalid phone number". | | `payerSocialId` | No | The buyer's ID number | | `payerZipCode` | No | The buyer's zip code | | `total` | Yes | `label`: non-empty text. `amount.currency`: non-empty, e.g. `ILS`. `amount.value`: a decimal string such as `'55.00'`, not minor units. Display only. | ```js { payerFirstName: 'Israel', payerLastName: 'Israeli', payerEmail: 'buyer@example.com', payerPhone: '0500000000', payerSocialId: '123456782', payerZipCode: '123456', total: { label: 'Order 1234', // A decimal string, not minor units. Display only. amount: { currency: 'ILS', value: '55.00' }, }, } ``` On success: ```js { type: 'tokenize-success', token: 'BUYERDEMO-BUYERDEMO-BUYERDEMO-1234567', testMode: true, card: { cardMask: '532610******5846', cardholderName: 'Israel Israeli', expiry: '1230' }, payerName: 'Israel Israeli', payerEmail: 'buyer@example.com', payerPhone: '0500000000', payerSocialId: '123456782', total: { label: 'Order 1234', amount: { currency: 'ILS', value: '55.00' } }, } ``` It rejects in one of four forms: | Cause | What you get | What to show | |---|---|---| | **Your data is wrong**: a missing or invalid value you passed, a bad `total`, or an unknown key | A plain `Error` with no `type`, e.g. `Please provide "payerEmail" value`, `Invalid email address`, `Invalid phone number`, `currency should not be empty` | `error.message`, or fix your code | | **A mounted field is empty** | `{ type: 'tokenize-error', validationError: true, errors: { payerEmail: 'Mandatory field' } }`. The text is the field's `messages.required`. | The message per field | | **PayMe refused the card**: an invalid number, an expired card, a decline… | `{ type: 'tokenize-error', code: 'ERR-355', message: 'Invalid credit card number', statusCode: 500, commStatus: 500, payload: { … }, timestamp: '…' }`. `code` is `ERR-` plus PayMe's error code for errors a buyer can act on, otherwise `ERR-020` with a generic message. | `message`. Don't show `payload` fields to the buyer | | **The instance already returned a token** | `{ type: 'tokenize-error', commStatus: 400, commStatusText: 'Bad Request' }` | Start from a new instance | A filled but invalid mounted field (for example a wrong card number) isn't caught in the browser: it comes back from PayMe as the third form. After a failed `tokenize()` you can call it again on the same instance. After a successful one, the instance is spent. ```js try { const result = await instance.tokenize(saleData); // send result.token to your server } catch (failure) { const text = Object.values(failure.errors ?? {})[0] ?? failure.message ?? 'Please try again.'; showError(text); } ``` ## Validators For your own payer inputs, `PayMe.validators` holds the rules `tokenize` applies to `payerFirstName`, `payerLastName`, `payerEmail`, `payerPhone`, `payerSocialId` and `payerZipCode`. ```js const problem = PayMe.validators[PayMe.fields.EMAIL].test('buyer@example.com'); // a falsy value when valid, { required: true } when empty, { invalid: true } when the format is wrong ``` Only the email and phone rules check the format. Names accept any non-empty value, and the social ID and zip code accept any value. ## Teardown `instance.teardown()` removes the instance's fields. Call it before you mount a new instance in the same containers.