Why is the figma.fileKey showing as undefined?

Is this a public plugin or the one published only to your Figma Organization? If it’s the former, you can’t use private plugin APIs. They are private because they are only available to private plugins that are not shared on Figma Community publicly. There is no way to get the file key for a public plugin other than ask the user to paste the link manually.