Post file to repo with API without encoding to base64

I am looking to see if there is a way to upload a new file to a repo using the API without having to encode the content of the file to base64 format. The data files are being created on an IoT type device and it does not appear that I have a great way to encode the content on that device before pushing it to GitHub. Any ideas or suggestions on approaches to try?

:wave: @christian-lorange Welcome to the GitHub Support Community, and thanks for asking about this here!

The GitHub REST API’s Contents API only has one endpoint for creating a new file and it expects that the new file content is encoded in Base64.

Aside from this endpoint, there’s also a Git Data API that gives you access to read and write raw Git objects to your Git database on GitHub, in addition to listing and updating your references (branch heads and tags). One approach that you could take is creating a blob: this endpoint has a content parameter and an encoding parameter, where encoding can either be utf-8 or base64. You could create a Git blob for which its contents are encoded in utf-8.

However, it’s worth pointing out that creating a Git blob in the repository is not the same as creating a file that can be accessed in the repository as if it were on a Git tree as introduced by a Git commit. Associating a blob with a Git tree and Git commit requires making more calls to the relevant endpoints documented here:

To get an idea of what that work looks like, the Git Data API documentation offers a great example for committing a change to an existing file in your repository using that interface.

Given this context, I don’t think there’s one “right” approach to go about it (to which I’d love to hear others thoughts on :smile_cat:). Between the Contents API and Git Data API, the Contents API offers a single endpoint that allows you to accomplish the goal of creating a file on a repository without having to consider the technical inner-workings of Git. Given that, you could have your IoT device send that data to a server or an application that you own. Once the data is offloaded there, you could write a script that encodes the data in base64 and create the file using the Contents API.