Put Cloud Storage
Cloud Storage & File SharingUsing the Put.io API with JavaScript
Put.io provides a public API that allows developers to interact with their cloud storage service programmatically. In this article, we will be discussing how to use this API using JavaScript, along with some sample code snippets.
Getting Started
To use the Put.io API, you will first need to create an account on their website and get an API key. Once you have your API key, you can begin making requests to the API by sending HTTP requests to its endpoints.
All requests to the API should be made using the HTTPS protocol, and all API responses will be in JSON format.
Making Requests
To make requests to the Put.io API using JavaScript, we can use the fetch
function, which is built into modern browsers and Node.js. Here is an example of how to fetch the list of files in your account:
const apiKey = 'your_api_key_here';
const url = `https://api.put.io/v2/files/list?oauth_token=${apiKey}`;
fetch(url)
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
In this example, we first define our API key and the API endpoint we want to call. We then use the fetch
function to send an HTTP GET request to this endpoint. Finally, we convert the response to JSON and log it to the console.
Authentication
To authenticate requests to the API, we need to include our API key as a query parameter in each request. For example, here is how to create a folder in your account using the API:
const apiKey = 'your_api_key_here';
const url = `https://api.put.io/v2/files/create-folder?oauth_token=${apiKey}`;
const formData = new FormData();
formData.append('name', 'My Folder');
fetch(url, {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error);
});
In this example, we create a new FormData
object that contains the folder name we want to create. We then send an HTTP POST request to the create-folder
endpoint, including our API key as a query parameter and our FormData
object as the request body.
Conclusion
With these examples, you should now have a basic understanding of how to use the Put.io API with JavaScript. To learn more about the API and its endpoints, you can refer to the official documentation on SwaggerHub.