## Create a Source

`stripe.createSource(element: object, sourceData: object)`

Use `stripe.createSource` to convert payment information collected by elements into a [Source](https://docs.stripe.com/api.md#sources) object that you safely pass to your server to use in an API call. See the [Sources documentation](https://docs.stripe.com/sources.md) for more information about sources.

- `element`
The [Element](https://docs.stripe.com/js/element.md) containing payment information. If applicable, the `Element` pulls data from other elements you’ve created on the same [Elements](#elements_create) instance.

- `sourceData`
A required object containing the `type` of `Source` you want to create, and any additional payment information that you have collected. See the [Sources API](https://docs.stripe.com/api.md#create_source) reference for details.

### Example

```javascript
const stripe = Stripe('pk_test_...');

const elements = stripe.elements();
const cardElement = elements.create('card');
cardElement.mount('#card-element');

const sourceData = {
  type: 'card',
  // Optionally, pass additional data that you have collected
  // card: {
  //   billing_details: {
  //     name: 'Jenny Rosen',
  //   },
  // },
};

const element = cardElement;

stripe.createSource({
  element,
  sourceData,
}).then(function(result) {
  if (result.error) {
    // Inform the user if there was an error.
    const errorElement = document.getElementById('error-message');
    errorElement.textContent = result.error.message;
  } else {
    // The Source object is in result.source
    // Send the Source ID to your server to create a charge
    // or attach it to a Customer.
  }
});
```