Magikarp being sold by the Magikarp salesman
James purchased his Gyarados as a Magikarp on the St. Anne in Battle Aboard the St. Anne. While on the ocean liner, the Magikarp salesman tricked him into buying Magikarp, claiming that he could breed it to sell more and more Magikarp. Blinded by the chance to become rich, James gladly accepted the deal. When he later told about his purchase to Jessie and Meowth, however, they revealed that he had been tricked: Magikarp was a completely useless Pokémon, and even its "gold" Poké Ball was just gold-plated. When James revealed that he had spent the advances on both his and Jessie's salaries to buy Magikarp, Jessie got furious, angrily telling him to return it immediately. However, before he could find the salesman again, Team Rocket's assault on the ship started, and James was forced to abandon his search to join the other Team Rocket Grunts. After the operation had been ruined by the passengers' rebellion, the rocking of the ship caused James to drop Magikarp's Poké Ball, and the trio kept chasing it throughout the ship, causing them to get trapped inside and knocked unconscious when the ship sunk soon after.
In Pokémon Shipwreck, when Team Rocket temporarily joined forces with Ash and his friends in order to escape the sunken ship, they appeared to lack a Water Pokémon to swim to the surface, at which point James revealed his Magikarp, believing that it could help them escape. Unfortunately, they soon found out that Magikarp wasn't able to swim at all, just splashing on its place. By sheer luck, the trio was carried to the surface when the ship flooded, along with Magikarp.
The next day, when the group was starving with hunger, they started staring at Magikarp, thinking of eating it. However, when Meowth tried to take a bite out of it, their dreams of a Magikarp lunch were ruined, as, like Misty then mentioned, Magikarp is all skin and bones, making it completely inedible. At this point, James got extremely enraged at Magikarp, declaring that it was completely useless and he didn't want to be its master anymore, and kicked it off the raft. This proved to be a big mistake, as it caused Magikarp to evolve into a Gyarados. Furious at James's behavior, Gyarados completely ignored his attempt to reclaim its loyalty and chased after the group. After it chased them for a while, it suddenly stopped and roared into the sky. Instead of giving up, however, as the group had hoped, this proved to be something much worse as multiple other Gyarados then appeared, and unleashed a devastating Dragon Rage on the group, sending them flying in a watery tornado. James's Gyarados hasn't appeared since having returned to living in the wild.
Gyarados reappeared as a Magikarp in James's flashbacks in Tricks of the Trade and Here's Lookin' at You, Elekid.