The chemical reactions and pathways involving heparin, any member of a group of glycosaminoglycans found mainly as an intracellular component of mast cells. They are similar to heparan sulfates but are of somewhat higher average Mr (6000-20000) and c.