1. Several key processes in freshwater ecology and evolution are governed by the connectivity inherent to dendritic river networks. These networks have extensively been analyzed from a geomorphological and hydrological viewpoint, yet network structures classically used in modelling have only been partially representative of the structure of real river basins, and have often failed to capture well known scaling features of real river networks. Pioneering work has identified optimal channel networks (OCNs) as spanning trees that reproduce all scaling features characteristic of real, natural stream networks worldwide. While these networks have been used to generate landscapes for studies on metapopulations, biodiversity and epidemiology, their generation has not been generally accessible.2. Given the increasing interest in dendritic riverine networks by ecologists and evolutionary biologists, we here present a method to generate OCNs and, to facilitate its application, we also provide the R-package OCNet. Owing to the random search process that generates OCNs, multiple network replicas spanning the same surface can be built, allowing one to perform computational experiments whose results do not depend on the particular shape of a single river network. The OCN construct also enables the generation of elevational gradients derived from the optimal network configuration, which can constitute three-dimensional landscapes for spatial studies in both terrestrial and freshwater realms. Moreover, the OCNet package provides functions that aggregate the OCN into an arbitrary number of nodes, calculate several metrics and descriptors of river networks, and draw relevant features of the network.3. We describe the main functionalities of the package and present how it can be integrated into other R-packages commonly used in spatial ecology. Moreover, we exemplify the generation of OCNs and discuss an application to a metapopulation model for an invasive riverine species. 4. In conclusion, OCNet provides a powerful tool to generate and use realistic river network analogues for various applications. It thereby allows the design of spatially realistic studies in increasingly impacted ecosystems, and enhances our knowledge on spatial processes in freshwater ecology in general.