Assuming you are using this api example. There are 2 tables in the DB reponsables for media libraries:
media_file. Examen them and see if the values that you are using for your api call are correct. Probably you are trying to insert a record with incorrect siteid, that is why you get constraint error.
mediaFile.FileSiteID = library.LibrarySiteID // get it from library instead of SiteContext.CurrentSiteID;
mediaFile.FileLibraryID = library.LibraryID;