How to make an API request with the SDK
masterThe SDK follows a pattern of initializing a configuration client, passing that client to a specific API object, and then calling a method on that object with a request body.
- Initialize Config: Create a
MercadoPagoConfiginstance with youraccessToken. - Initialize API Object: Instantiate the desired API class (e.g.,
Order) by passing theclientinstance. - Define Body: Construct the request payload object (e.g., for an Order).
- Execute: Call the API method (e.g.,
.create()) passing an object containing thebodyand optionalrequestOptions.
import { MercadoPagoConfig, Order } from "mercadopago";
// 1. Initialize client
const client = new MercadoPagoConfig({
accessToken: "<ACCESS_TOKEN>",
});
// 2. Initialize API object
const order = new Order(client);
// 3. Create request body
const body = {
type: "online",
total_amount: "1000.00",
// ... other fields
};
// 4. Make the request
order.create({ body }).then(console.log).catch(console.error);