We introduce the notion of fundamental heap for compact orientable surfaces with boundary embedded in $3$-space, which is an isotopy invariant of the embedding. It is a group, endowed with a ternary heap operation, defined using diagrams of surfaces in a form of thickened trivalent graphs called surface ribbons. We prove that the fundamental heap has a free part whose rank is given by the number of connected components of the surface. We study the behavior of the invariant under boundary connected sum, as well as addition/deletion of twisted bands, and provide formulas relating the number of generators of the fundamental heap to the Euler characteristics. We describe in detail the effect of stabilization on the fundamental heap, and determine that for each given finitely presented group there exists a surface ribbon whose fundamental heap is isomorphic to it, up to extra free factors. A relation between the fundamental heap and the Wirtinger presentation is also described. Moreover, we introduce cocycle invariants for surface ribbons using the notion of mutually distributive cohomology and heap colorings. Explicit computations of fundamental heap and cocycle invariants are presented.