Trash Nothing

Social

Working with the TrashNothing Public API

TrashNothing is a free and open-source platform that allows users to give away, trade, or sell items that they no longer need or want. The platform includes a public API that developers can use to interact with the database and access information about items and users. In this article, we will provide a brief overview of how to work with the TrashNothing Public API using JavaScript.

Authentication

The TrashNothing API requires authentication via an API key to access its endpoints. To authenticate, you need to obtain an API key by signing up at https://trashnothing.com/developer. Once you have an API key, you must include it in the headers of your requests, using the following format:

const API_KEY = 'your-api-key-here';
const headers = {
  'Authorization': `Api-Key ${API_KEY}`,
  'Content-Type': 'application/json',
};

Endpoints

The TrashNothing Public API has several endpoints that allow you to access different types of data, such as items, users, and categories. Here are a few examples of how to use these endpoints in JavaScript.

Search for Items

To search for items on TrashNothing, you can use the GET /api/v1/free endpoint, which returns a list of free items that match the specified search criteria. Here is an example of how to use this endpoint in JavaScript:

const search = 'bicycle';
const limit = 10;
const url = `https://trashnothing.com/api/v1/free?search=${search}&limit=${limit}`;
const response = await fetch(url, { headers });
const data = await response.json();
console.log(data);

In this example, we're searching for the term "bicycle" and limiting the results to 10 items. We're using the fetch() method to send a GET request with our headers and the search parameters in the URL. The response is returned as JSON, which we parse and log to the console.

Get Item Details

To get the details of a specific item on TrashNothing, you can use the GET /api/v1/free/{item_id} endpoint. Here is an example of how to use this endpoint in JavaScript:

const item_id = 1234;
const url = `https://trashnothing.com/api/v1/free/${item_id}`;
const response = await fetch(url, { headers });
const data = await response.json();
console.log(data);

In this example, we're fetching the details of an item with the ID "1234". We're using the fetch() method to send a GET request with our headers and the item ID in the URL. The response is returned as JSON, which we parse and log to the console.

Get User Profile

To get the profile details of a specific user on TrashNothing, you can use the GET /api/v1/user/{user_id} endpoint. Here is an example of how to use this endpoint in JavaScript:

const user_id = 5678;
const url = `https://trashnothing.com/api/v1/user/${user_id}`;
const response = await fetch(url, { headers });
const data = await response.json();
console.log(data);

In this example, we're fetching the profile details of a user with the ID "5678". We're using the fetch() method to send a GET request with our headers and the user ID in the URL. The response is returned as JSON, which we parse and log to the console.

Conclusion

In this article, we provided a brief overview of how to work with the TrashNothing Public API using JavaScript. We showed some examples of how to authenticate, use endpoints to search for items, get item details, and get user profiles. For more information, you can refer to the API documentation available at https://trashnothing.com/developer.

Related APIs in Social