An open source medusa sdk for golang
- Harsh Mangalam
You can install Medusa by either following our Quickstart guide or the following steps:
-
Install Medusa CLI
npm install -g @medusajs/medusa-cli
-
Create a new Medusa project
medusa new my-medusa-store --seed
-
Start your Medusa engine
cd my-medusa-store medusa develop
go get github.com/harshmangalam/medusa-sdk-golang@latest
config := medusa.NewConfig().
SetMaxRetries(3).
SetBaseUrl("http://localhost:9000")
Auth endpoints that allow authorization of customers and manages their sessions
Logs a Customer in and authorizes them to view their details. Successful authentication will set a session cookie in the Customer's browser.
resp, err := auth.NewAuth().
SetEmail("harsh@gmail.com").
SetPassword("123456").
Authenticate(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Destroys a Customer's authenticated session.
data, err := auth.DeleteSession(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(data.Data)
fmt.Println(data.Error)
fmt.Println(data.Errors)
Gets the currently logged in Customer.
data, err := auth.GetSession(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(data.Data)
fmt.Println(data.Error)
fmt.Println(data.Errors)
Checks if a Customer with the given email has signed up.
data, err := auth.Exists("harsh@gmail.com", config)
if err != nil {
fmt.Println(err)
}
fmt.Println(data.Data)
fmt.Println(data.Error)
fmt.Println(data.Errors)
Cart allow handling carts in Medusa.
Adds a Shipping Method to the Cart.
resp, err := carts.NewShippingMethod().
SetOptionId(optionId).
SetData(data).
Add(cartId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Completes a cart. The following steps will be performed. Payment authorization is attempted and if more work is required, we simply return the cart for further updates. If payment is authorized and order is not yet created, we make sure to do so. The completion of a cart can be performed idempotently with a provided header Idempotency-Key. If not provided, we will generate one for the request.
resp, err := carts.Complete(cartId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Creates Payment Sessions for each of the available Payment Providers in the Cart's Region.
resp, err := carts.CreatePaymentSession(cartId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Creates a Cart within the given region and with the initial items. If no region_id is provided the cart will be associated with the first Region available. If no items are provided the cart will be empty after creation. If a user is logged in the cart's customer id and email will be set.
resp, err := carts.NewCreateCart().
SetCountryCode(countryCode).
SetItems(items).
SetRegionId(regionId).
SetSalesChannelId(salesChannelId).
SetContext(cartContext).
Create(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data.Cart)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Removes a Discount from a Cart.
resp, err := carts.DeleteDiscount(cartId, code, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Deletes a Payment Session on a Cart. May be useful if a payment has failed.
resp, err := carts.DeletePaymentSession(cartId, providerId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Refreshes a Payment Session to ensure that it is in sync with the Cart - this is usually not necessary.
resp, err := carts.RefreshPaymentSession(cartId, providerId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves a Cart.
resp, err := carts.Retrieve(cartId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Selects a Payment Session as the session intended to be used towards the completion of the Cart.
resp, err := carts.NewSelectPaymentSession().
SetProviderId(providerId).
Select(crtId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Updates a Payment Session with additional data.
resp, err := carts.NewUpdatePaymentSession().
SetData(data).
Update(cartId, providerId, confi)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Updates a Cart.
resp, err := carts.NewUpdateCart().
SetBillingAddress(billingAddress).
SetContext(context).
SetCountryCode(countryCode).
SetCustomerId(customerId).
SetDiscounts(discount).
SetEmail(email).
SetGiftCards(giftCards).
SetRegionId(regionId).
SetSalesChannelId(salesChannelId).
SetShippingAddress(shippingAddress).
Update(cartId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Collection allow handling collections in Medusa.
Retrieves a Product Collection.
resp, err := collections.Retrieve(id, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieve a list of Product Collection.
createdAt := common.NewDateComparison().
SetLte(time).
SetGt(time).
SetGte(time).
SetLt(time)
updatedAt := common.NewDateComparison().
SetLte(time).
SetGt(time).
SetGte(time).
SetLt(time)
resp, err := collections.
NewCollectionsQuery().
SetLimit(4).SetOffset(2).
SetCreatedAt(createdAt).
SetUpdatedAt(updatedAt).
List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Customer endpoints that allow handling customers in Medusa.
Creates a Customer account.
resp, err := customers.
NewCreateCustomer().
SetFirstName(firstname).
SetLastName(lastname).
SetEmail(email).
SetPassword(password).
Create(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves a list of a Customer's Orders.
resp, err := customers.NewListOrderQuery().
SetCancledAt(dateComparison).
SetCartId(cartId).
SetCreatedAt(dateComparison).
SetCurrencyCode(currencyCode).
SetEmail(email).
SetExpand(expand).
SetFields(fields).
SetFulfillmentStatus(fulfillmentStatus).
SetId(id).
SetLimit(limit).
SetOffset(offset).
SetPaymentStatus(paymentStatus).
SetQ(q).
SetRegionId(regionId).
SetStatus(status).
SetTaxRate(taxRate).
SetUpdatedAt(dateComparison).
List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Creates a reset password token to be used in a subsequent /reset-password request. The password token should be sent out of band e.g. via email and will not be returned.
resp, err := customers.NewRequestPasswordReset().
SetEmail(email).
RequestReset(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Resets a Customer's password using a password token created by a previous /password-token request.
resp, err := customers.
NewResetPassword().
SetEmail(email).
SetPassword(password).
SetToken(token).
Reset(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves a Customer - the Customer must be logged in to retrieve their details.
resp, err := customers.Retrieve(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Updates a Customer's saved details.
resp, err := customers.
NewUpdateCustomer().
SetFirstName(firstname).
SetLastName(lastname).
SetBillingAddress(address).
SetMetadata(metaData).
SetPassword(password).
SetPhone(phone).
SetEmail(email).
Update(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Gift Card endpoints that allow handling gift cards in Medusa.
Retrieves a Gift Card by its associated unqiue code.
resp, err := giftcards.Retrieve(code, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Order allow handling orders in Medusa.
Retrieves an Order by the id of the Cart that was used to create the Order.
resp, err := orders.RetrieveByCartId(cartId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Look up an order using filters.
resp, err := orders.NewLookup().
SetDisplayId(displayId).
SetEmail(email).
SetShippingAddress(address).
Lookup(cartId, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves an Order
resp, err := orders.Retrieve(id, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Completes an OrderEdit.
resp, err := orderedits.Complete(id, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Declines an OrderEdit.
resp, err := orderedits.NewDeclineOrderEdit().
SetDeclineReason(declineReason).
Decline(id, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves a OrderEdit.
resp, err := orderedits.Retrieve(id, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves a Product.
resp, err := products.Retrieve(id, config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves a list of Products.
resp, err := products.NewListProduct().
SetCollectionIds(collectionsId).
SetCreatedAt(createdAt).
SetDescription(desc).
SetExpand(expand).
SetFields(fields).
SetHandle(handle).
SetIds(ids).
SetIsGiftcard(isGiftCard).
SetLimit(limit).
SetOffset(offset).
SetQ(q).
SetTags(tags).
SetTitle(title).
SetType(type).
SetUpdatedAt(updatedAt).
List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Run a search query on products using the search engine installed on Medusa
resp, err := products.NewSearchProduct().
SetLimit(limit).
SetOffset(offset).
SetQ(q).
Search(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Retrieves a Product Variant by id
resp, err := products.RetrieveVariant(variantId,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Get Product Variants
resp, err := products.NewListProuductVariant().
SetExpand(expand).
SetIds(ids).
SetInventoryQuantity(invQty).
SetLimit(limit).
SetOffset(offset).
SetTitle(title).
List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := regions.Retrieve(regionId,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := regions.NewListRegion().
SetCreatedAt(createdAt).
SetLimit(limit).
SetOffset(offset).
SetUpdatedAt(updatedAt).
List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp,err := returns.NewCreateRetun().
SetItems(items).
SetOrderId(orderId).
SetReturnShipping(returnShippings).
Create(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := returnreasons.Retrieve(id,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := returnreasons.List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := shippingoptions.NewListShippingOption().
SetIsreturn(isReturn).
SetProductIds(productIds).
SetRegionId(regionId).
List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := shippingoptions.ListCartOptions(cartId,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := swaps.NewCreateSwap().
SetAdditionalItems(additionalItems).
SetOrderId(orderId).
SetReturnItems(returnItems).
SetReturnShippingOption(returnShippingOptions).
Create(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
resp, err := swaps.RetrieveByCartId(cartId,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Licensed under the MIT License