Skip to main content
Add, test, and deploy Clarity contracts with auto-generated TypeScript bindings.

Contents

Creating contracts

Use the CLI to add new contracts to your project:

Sample contract

Here’s a complete Clarity contract that demonstrates key concepts including sBTC payments, data storage, and read-only functions. This is based on the official Stacks Developer Quickstart.

Contract requirements

For contracts that depend on external contracts (like sBTC), add requirements:
This wires up the official contracts for local testing and deployment.

Key concepts

sBTC Integration

This contract uses sBTC (Stacks’ 1:1 backed Bitcoin) for payments:
  • Requires the sBTC contract as a dependency
  • Uses restrict-assets? for post-conditions
  • Transfers sBTC using the official token contract

Data Storage

  • Maps: Store structured data (messages with metadata)
  • Data variables: Track counters and global state
  • Constants: Define immutable values

Access Control

  • CONTRACT_OWNER for privileged operations
  • asserts! for authorization checks
  • tx-sender vs contract-caller for security

Read-Only Functions

  • Query contract state without transactions
  • Use at-block for historical data
  • Return Optional types for safe lookups

Testing contracts

Write tests in contracts/tests/ using the Clarinet SDK (3.21+). Full patterns, ABI caching, and frontend Vitest coverage live in Testing.