> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bitgpt.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Accept a payment

<Card title="API Keys" icon="key" href="/developer-resources/get-started/api-keys" arrow="true">
  Retrieve or create yur API Key from the dashboard. Learn more about it here.
</Card>

<Steps>
  <Step title="Create a payment" icon="receipt">
    Use this simple payment intent payload to create a payment for any arbitrary amount.

    <CardGroup cols={2}>
      <Card title="Currencies" icon="dollar-sign" href="/developer-resources/schemas/currency" arrow="true">
        Your customers can pay you with any currency of their choice, we automatically settle it for you.
      </Card>

      <Card title="Idempotency" icon="fingerprint" href="/developer-resources/get-started/idempotency" arrow="true">
        We support idempotent requests to help you retry safely without creating duplicate payments.
      </Card>

      <Card title="Payment schema" icon="brackets-curly" href="/developer-resources/reference/payments/create-an-invoice" arrow="true">
        The create invoice API supports many more parameters to adapt to your business needs, read more here.
      </Card>

      <Card title="Redirect your customer" icon="arrow-up-right-from-square" href="/recipes/redirect-checkout" arrow="true">
        You can pass `redirect_url` to send your customers to a custom link. It supports dynamic data as well.
      </Card>
    </CardGroup>

    Example payload used; metadata can be changed (or removed) to any key-value items of your choice.

    ```json theme={"system"}
    POST https://api.bitgpt.xyz/invoices

    {
      customer_email: "example@gmail.com",
      items: [
        {
          type: "PAYMENT_INTENT",
          currency: "GBP",
          price: "10"
        }
      ],
      metadata: {
        any_custom_key_1: "any_custom_value_1",
        any_custom_key_2: "any_custom_value_2"
      }
    }
    ```

    <CodeGroup dropdown>
      ```bash cURL wrap lines expandable theme={"system"}
      curl -X POST https://api.bitgpt.xyz/invoices \
        -H "Authorization: Bearer YOUR_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d '{
          "customer_email": "example@gmail.com",
          "items": [
            {
              "type": "PAYMENT_INTENT",
              "currency": "GBP",
              "price": "10"
            }
          ],
          "metadata": {
            "any_custom_key_1": "any_custom_value_1",
            "any_custom_key_2": "any_custom_value_2"
          }
        }'
      ```

      ```javascript Example.js wrap lines expandable theme={"system"}
      const { v4: uuidv4 } = require('uuid');
      const fetch = require('node-fetch');

      const idempotencyKey = uuidv4();

      fetch('https://api.bitgpt.xyz/invoices', {
        method: 'POST',
        headers: {
          'Authorization': 'Bearer YOUR_API_KEY',
          'Content-Type': 'application/json',
          'Idempotency-Key': idempotencyKey
        },
        body: JSON.stringify({
          customer_email: "example@gmail.com",
          items: [
            {
              type: "PAYMENT_INTENT",
              currency: "GBP",
              price: "10"
            }
          ],
          metadata: {
            any_custom_key_1: "any_custom_value_1",
            any_custom_key_2: "any_custom_value_2"
          }
        })
      })
      .then(res => res.json())
      .then(data => console.log(data));
      ```

      ```typescript Example.ts wrap lines expandable theme={"system"}
      import fetch from 'node-fetch';
      import { v4 as uuidv4 } from 'uuid';

      const payload = {
        customer_email: "example@gmail.com",
        items: [
          {
            type: "PAYMENT_INTENT",
            currency: "GBP",
            price: "10"
          }
        ],
        metadata: {
          any_custom_key_1: "any_custom_value_1",
          any_custom_key_2: "any_custom_value_2"
        }
      };

      async function createInvoice() {
        const res = await fetch("https://api.bitgpt.xyz/invoices", {
          method: "POST",
          headers: {
            "Authorization": "Bearer YOUR_API_KEY",
            "Content-Type": "application/json",
            "Idempotency-Key": uuidv4()
          },
          body: JSON.stringify(payload)
        });

        const data = await res.json();
        console.log(data);
      }

      createInvoice();
      ```

      ```php Example.php wrap lines expandable theme={"system"}
      <?php

      $payload = json_encode([
        "customer_email" => "example@gmail.com",
        "items" => [
          [
            "type" => "PAYMENT_INTENT",
            "currency" => "GBP",
            "price" => "10"
          ]
        ],
        "metadata" => [
          "any_custom_key_1" => "any_custom_value_1",
          "any_custom_key_2" => "any_custom_value_2"
        ]
      ]);

      $idempotencyKey = bin2hex(random_bytes(16));

      $ch = curl_init('https://api.bitgpt.xyz/invoices');
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer YOUR_API_KEY',
        'Content-Type: application/json',
        'Idempotency-Key: ' . $idempotencyKey
      ]);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

      $response = curl_exec($ch);
      curl_close($ch);
      echo $response;

      ?>
      ```

      ```python Example.py wrap lines expandable theme={"system"}
      import requests
      import uuid

      payload = {
        "customer_email": "example@gmail.com",
        "items": [
          {
            "type": "PAYMENT_INTENT",
            "currency": "GBP",
            "price": "10"
          }
        ],
        "metadata": {
          "any_custom_key_1": "any_custom_value_1",
          "any_custom_key_2": "any_custom_value_2"
        }
      }

      headers = {
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
        "Idempotency-Key": str(uuid.uuid4())
      }

      response = requests.post("https://api.bitgpt.xyz/invoices", json=payload, headers=headers)
      print(response.json())
      ```

      ```java Example.java wrap lines expandable theme={"system"}
      import java.io.OutputStream;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.util.UUID;

      public class CreateInvoice {
        public static void main(String[] args) throws Exception {
          URL url = new URL("https://api.bitgpt.xyz/invoices");
          HttpURLConnection con = (HttpURLConnection) url.openConnection();
          con.setRequestMethod("POST");
          con.setRequestProperty("Authorization", "Bearer YOUR_API_KEY");
          con.setRequestProperty("Content-Type", "application/json");
          con.setRequestProperty("Idempotency-Key", UUID.randomUUID().toString());
          con.setDoOutput(true);

          String jsonInputString = """
            {
              "customer_email": "example@gmail.com",
              "items": [
                {
                  "type": "PAYMENT_INTENT",
                  "currency": "GBP",
                  "price": "10"
                }
              ],
              "metadata": {
                "any_custom_key_1": "any_custom_value_1",
                "any_custom_key_2": "any_custom_value_2"
              }
            }
          """;

          try (OutputStream os = con.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
          }

          java.io.InputStream is = con.getInputStream();
          byte[] response = is.readAllBytes();
          System.out.println(new String(response));
        }
      }
      ```

      ```go Example.go wrap lines expandable theme={"system"}
      package main

      import (
        "bytes"
        "crypto/rand"
        "encoding/hex"
        "fmt"
        "io"
        "net/http"
      )

      func generateUUIDv4() string {
        b := make([]byte, 16)
        rand.Read(b)
        b[6] = (b[6] & 0x0f) | 0x40 // Version 4
        b[8] = (b[8] & 0x3f) | 0x80 // Variant
        return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
          b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
      }

      func main() {
        jsonStr := []byte(`{
          "customer_email": "example@gmail.com",
          "items": [
            {
              "type": "PAYMENT_INTENT",
              "currency": "GBP",
              "price": "10"
            }
          ],
          "metadata": {
            "any_custom_key_1": "any_custom_value_1",
            "any_custom_key_2": "any_custom_value_2"
          }
        }`)

        req, err := http.NewRequest("POST", "https://api.bitgpt.xyz/invoices", bytes.NewBuffer(jsonStr))
        if err != nil {
          panic(err)
        }

        req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
        req.Header.Set("Content-Type", "application/json")
        req.Header.Set("Idempotency-Key", generateUUIDv4())

        client := &http.Client{}
        resp, err := client.Do(req)
        if err != nil {
          panic(err)
        }
        defer resp.Body.Close()

        body, _ := io.ReadAll(resp.Body)
        fmt.Println(string(body))
      }
      ```

      ```rust Example.rs wrap lines expandable theme={"system"}
      use reqwest::blocking::Client;
      use serde_json::json;
      use uuid::Uuid;

      fn main() -> Result<(), Box<dyn std::error::Error>> {
          let client = Client::new();

          let payload = json!({
              "customer_email": "example@gmail.com",
              "items": [
                  {
                      "type": "PAYMENT_INTENT",
                      "currency": "GBP",
                      "price": "10"
                  }
              ],
              "metadata": {
                  "any_custom_key_1": "any_custom_value_1",
                  "any_custom_key_2": "any_custom_value_2"
              }
          });

          let idempotency_key = Uuid::new_v4().to_string();

          let res = client
              .post("https://api.bitgpt.xyz/invoices")
              .header("Authorization", "Bearer YOUR_API_KEY")
              .header("Content-Type", "application/json")
              .header("Idempotency-Key", idempotency_key)
              .json(&payload)
              .send()?
              .text()?;

          println!("{}", res);
          Ok(())
      }
      ```
    </CodeGroup>
  </Step>

  <Step title="Redirect to checkout" icon="arrow-up-right-from-square">
    Redirect the customer to `https://pay.bitgpt.xyz/{response.data.id}`

    <Info>The invoice id at `response.data.id` will look like this: `invoice_0197c626-8ab2-7ab6-8a20-78ae53a606d3`</Info>
  </Step>

  <Step title="Handle webhooks" icon="webhook">
    <Card title="Webhook handling recipe" icon="hat-chef" href="/recipes/handle-webhook" arrow="true">
      Use the recipe for webhook handling to listen to status changes and invoice completion.
    </Card>
  </Step>
</Steps>
