{"schema_version":"1.0","publisher":"Primitive SDKs","canonical_url":"https://test.abhinandan.one/x402-payments-overview-04a296ff/go-x402-create-charge","markdown_url":"https://test.abhinandan.one/x402-payments-overview-04a296ff/go-x402-create-charge.md","article":{"id":"f6cea75a-d8b4-4b24-ae87-d9e8cf4008cb","article_slug":"go-x402-create-charge","parent_article_slug":"x402-payments-overview-04a296ff","parent_article_title":"x402 Payments Overview","kind":"guide","published_at":"2026-08-11T18:54:58.265907+00:00","keywords":["Client.Charge","X402ChargeInput","x402 payment challenge","AmountUsdc","GetChallenge","PayerOrg"],"meta_description":"Client.Charge creates an x402 payment challenge as the payee, accepting either a human AmountUsdc string or a base-unit Amount for the Go SDK.","og_image_url":null,"source_file_paths":["sdk-go/x402.go","sdk-go/x402_test.go","sdk-go/README.md"],"recording_id":null,"replayable":false,"task_name":"Creating a Payment Challenge (Go SDK)","category":"Go SDK","summary":null,"description":"Create an x402 payment challenge as the payee with Client.Charge, specifying the amount as human USDC or raw token base units, then hand the challenge to the payer.","content_kind":"repo_page","content_markdown":"Use `Client.Charge` when your agent is the payee and needs to request a USDC payment from another agent. It creates an [x402 payment challenge](x402-payments-overview) that the payer signs and settles with `Pay`.\n\nBefore calling `Charge`, register a payout address once with [`RegisterPayoutAddress`](go-x402-register-payout). `Charge` resolves `pay_to` from that registration, so a charge without one fails.\n\n<Note>\n\nThis page covers the payee side (creating the challenge). The payer settles it with `client.Pay(ctx, challenge, signer)`; for the shared payment model, see [x402 Payments Overview](x402-payments-overview). For a payment that rides a real email thread instead of a synthetic id, see the email-native flow on the same page.\n\n</Note>\n\n## Construct the x402 client\n\n`Charge` is a method on `*primitive.X402Client`, the x402 payments client built with `NewX402Client`; client construction and the shared payment model are explained in [x402 Payments Overview](x402-payments-overview). With zero options it reads `PRIMITIVE_API_KEY` from the environment and targets the production host (`https://api.primitive.dev`).\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"log\"\n\t\"os\"\n\n\tprimitive \"github.com/primitivedotdev/sdks/sdk-go\"\n)\n\nfunc main() {\n\tctx := context.Background()\n\n\tclient := primitive.NewX402Client(primitive.X402ClientOptions{\n\t\tAPIKey: os.Getenv(\"PRIMITIVE_API_KEY\"),\n\t})\n\n\t_, err := client.Charge(ctx, primitive.X402ChargeInput{\n\t\tAmountUsdc: \"0.01\",\n\t\tNetwork:    \"base-sepolia\",\n\t})\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n## Create the challenge\n\n<Steps>\n\n<Step title=\"Choose an amount format\">\n\nSet exactly one of `AmountUsdc` (a human USDC decimal string like `\"0.01\"`, the documented easy path) or `Amount` (token base units, e.g. `\"10000\"`); see [x402 Payments Overview](x402-payments-overview) for how the two formats relate.\n\n</Step>\n\n<Step title=\"Call Client.Charge\">\n\n```go\nchallenge, err := client.Charge(ctx, primitive.X402ChargeInput{\n\tAmountUsdc:  \"0.01\", // human USDC amount\n\tNetwork:     \"base-sepolia\",\n\tPayerOrg:    os.Getenv(\"PAYER_ORG_ID\"), // org allowed to pay\n\tDescription: \"API call\",\n})\nif err != nil {\n\tlog.Fatal(err)\n}\n```\n\n`Network` is `\"base-sepolia\"` (testnet) or `\"base\"` (mainnet). `PayerOrg` binds the challenge to a specific paying org and is optional. `Description` is a free-text label surfaced back to the payer.\n\n</Step>\n\n<Step title=\"Hand the challenge to the payer\">\n\n`challenge` (a `*X402Challenge`) carries `payment_requirements` and `nonce_binding`, the exact fields the payer's `Pay` call signs over. Deliver it over any out-of-band channel (API response, dashboard, message).\n\n</Step>\n\n</Steps>\n\n## Re-hydrate a challenge later\n\n`GetChallenge` re-hydrates an existing challenge by id, so you can retry `Pay` after a process restart without creating a duplicate:\n\n```go\nchallenge, err := client.GetChallenge(ctx, challengeID)\nif err != nil {\n\tlog.Fatal(err)\n}\n```\n\n<Tip>\n\nNeed the challenge to ride a real email thread instead of a synthetic id? Use `CreateEmailChallenge` from the email-native flow described on [x402 Payments Overview](x402-payments-overview) instead of `Charge`.\n\n</Tip>\n\n<Warning>\n\nSetting both `AmountUsdc` and `Amount` on the same `X402ChargeInput`, or neither, is rejected before any network call is made. Set exactly one.\n\n</Warning>\n\n## Errors\n\n`Charge` returns a `*primitive.X402Error` on any client-side, transport, or non-2xx server error, the shared x402 error shape described in [x402 Payments Overview](x402-payments-overview):\n\n```go\nimport (\n\t\"errors\"\n\t\"log\"\n\n\tprimitive \"github.com/primitivedotdev/sdks/sdk-go\"\n)\n\nchallenge, err := client.Charge(ctx, primitive.X402ChargeInput{Amount: \"10000\"})\nif err != nil {\n\tvar x402Err *primitive.X402Error\n\tif errors.As(err, &x402Err) {\n\t\tlog.Printf(\"charge failed: status=%d retryAfter=%v\", x402Err.Status, x402Err.RetryAfter)\n\t}\n\treturn\n}\n_ = challenge\n```\n\nCommon `Charge`-time rejections, all local (no network call made):\n\n- Malformed or missing amount: `Amount` must be a positive integer string; `AmountUsdc` must be a positive decimal with at most 6 decimal places.\n- Both `Amount` and `AmountUsdc` set, or neither set.\n\nSee [x402 Errors](go-x402-errors) for the full status-code reference, including retry-after handling and indeterminate-outcome cases.\n\n## Next steps\n\n<CardGroup cols={2}>\n\n<Card title=\"x402 Payments Overview\" href=\"x402-payments-overview\">\n\nUnderstand the full payout-registration, charge, pay, and spend-policy model shared across SDKs.\n\n</Card>\n\n<Card title=\"Registering a Payout Address\" href=\"go-x402-register-payout\">\n\nProve control of a wallet and register it before your first Charge call.\n\n</Card>\n\n<Card title=\"x402 Errors\" href=\"go-x402-errors\">\n\nLook up X402Error status codes and indeterminate-outcome handling.\n\n</Card>\n\n<Card title=\"x402 Spend Policy\" href=\"go-x402-spend-policy\">\n\nGuard outbound payments with caps, an allowlist, and a kill-switch.\n\n</Card>\n\n</CardGroup>","canonical_base_url":"https://test.abhinandan.one","seo_indexing_enabled":true,"last_modified":"2026-08-21T18:22:43.359885+00:00","video_url":null,"voiceover_url":null,"tools_used":[],"demonstrated_by":[],"steps":[],"related_links":[],"intro":null,"prerequisites":[],"verification":[],"troubleshooting":[],"suggest_edit_url":"https://github.com/abhi-browzer/primitive-sdks/edit/main/sdk-go/x402.go","raise_issue_url":"https://github.com/abhi-browzer/primitive-sdks/issues/new?title=Docs+feedback%3A+Creating+a+Payment+Challenge+%28Go+SDK%29&body=Page%3A+https%3A%2F%2Ftest.abhinandan.one%2Fgo-x402-create-charge","page_feedback_enabled":true,"verified_ref":null,"verified_at":"2026-08-11T18:38:45.205849+00:00"}}