Abstract Molecular robots (nanobots) are being developed for biomedical applications, e.g., to deliver medications without worrying about side-effects. Future treatments will require swarms of heterogeneous nanobots We present a novel approach to generating such swarms from a treatment program. A compiler translates medications, written in a rulebased language, into specififications of a swarm built by specializing generic nanobot platforms to specifific payloads and action-triggering behavior. The mixture of nanobots, when deployed, carries out the treatment program. We describe the medication programming language, and the associated compiler. We prove the validity of the compiler output, and report on in-vitro experiments using generated nanobot swarms.