Medusa SDK Golang
An open source medusa sdk for golang
Author
Resources
Contents
Getting Started
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
Installation
required go v1.18+
go get github.com/vincent-vade/medusa-sdk-golang@latest
Configuration
config := medusa.NewConfig().
SetMaxRetries(3).
SetBaseUrl("http://localhost:9000")
Auth
Auth endpoints that allow authorization of customers and manages their sessions
Customer Login
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)
Customer Log out
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)
Get Current Customer
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)
Check if email exists
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
Cart allow handling carts in Medusa.
Add a Shipping Method
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)
Complete a Cart
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)
Create Payment Sessions
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)
Create a Cart
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)
Remove Discount
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)
Delete a Payment Session
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)
Refresh a Payment Session
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)
Get a Cart
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)
Select a Payment Session
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)
Update a Payment Session
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)
Update a Cart
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
Collection allow handling collections in Medusa.
Get a Collection
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)
List Collections
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
Customer endpoints that allow handling customers in Medusa.
Create a Customer
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)
List Orders
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)
Request Password Reset
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)
Reset Password
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)
Get a Customer
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)
Update Customer
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
Gift Card endpoints that allow handling gift cards in Medusa.
Get Gift Card by Code
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
Order allow handling orders in Medusa.
Get by Cart ID
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
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)
Get an Order
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)
OrderEdit
Completes an OrderEdit
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)
Decline an OrderEdit
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)
Retrieve an OrderEdit
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)
Product
Get a Product
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)
List Products
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)
Search Products
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)
Get a Product Variant
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)
Retrieves a list of Product Variants
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)
Region
Get a Region
resp, err := regions.Retrieve(regionId,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
List Regions
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)
Return
Create Return
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)
Return Reason
Get a Return Reason
resp, err := returnreasons.Retrieve(id,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
List Return Reasons
resp, err := returnreasons.List(config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Shipping Option
Get Shipping Options
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)
List for Cart
resp, err := shippingoptions.ListCartOptions(cartId,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
Swap
Create a Swap
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)
Get by Cart ID
resp, err := swaps.RetrieveByCartId(cartId,config)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Data)
fmt.Println(resp.Error)
fmt.Println(resp.Errors)
License
Licensed under the MIT License