1

I want to add a new DNS zone in OVH with Python using the OVH API.

I wrote a script with these steps:

  • Create a new cart
  • Add a new DNS zone to the cart
  • Check the cart content
  • Confirm the order
  • Check the order status

Did I forget a step or is there an error somewhere? Because when I look in GET orders I don't see new orders and also they don't appear in the GUI.

cart = client.post('/order/cart', ovhSubsidiary='PL')

#Get the cart ID
cart_id = cart.get('cartId')

#Set data for the new DNS zone
zone_name = 'testttt.pl' # DNS zone name

#Add the new DNS zone to the cart
result = client.post(f'/order/cart/{cart_id}/dns',
domain=zone_name,
duration="P1Y",
planCode="zone",
pricingMode="default",
quantity=1)

#Check if the operation was successful
if 'itemId' in result:
    print(f'DNS zone {zone_name} was added to the cart.')
else:
    print('Error while adding DNS zone to the cart.')

#Display the cart contents
cart_info = client.get(f'/order/cart/{cart_id}')
print(f'Cart contents:\n{json.dumps(cart_info, indent=4)}')

#Make sure the cart is ready to order
order = client.post(f'/order/cart/{cart_id}/checkout', autoPayWithPreferredPaymentMethod=True)
print(f'Order {order.get("orderId")} has been placed.')

order_id = cart_info['orders'][-1]

#Check the status of the order
order_status = client.get(f'/me/order/{order_id}/status')
print(f'Order {order_id} status: {order_status}')```
sphtd97
  • 53
  • 8

1 Answers1

1

After creating the cart, and before adding your DNS zone (or any other products) to this cart, you need to assign this cart to yourself.

It can be done with this route:

# Assign a shopping cart to an loggedin client
POST /order/cart/{cartId}/assign

So your script should looks like:

import ovh

# For these 3 keys, refer to the documentation:
# - https://github.com/ovh/python-ovh#use-the-api-on-behalf-of-a-user
# - Generate the keys easily via https://www.ovh.com/auth/api/createToken

client = ovh.Client(
    endpoint='ovh-eu',
    application_key='<app_key>',
    application_secret='<app_secret>',
    consumer_key='<consumer_key>'
)
# Initiate a new cart
cart = client.post(
    '/order/cart',
    ovhSubsidiary='PL'
)
cart_id = cart.get('cartId')

# Assign this cart to the currently logged-in user
assign = client.post(
    f'/order/cart/{cart_id}/assign'
)

zones_to_order = ['domain-1.ovh', 'domain-2.ovh', 'domain-3.ovh']
for domain in zones_to_order:
    # Add a new DNS zone to the cart
    result = client.post(
        f'/order/cart/{ cart_id }/dns',
        duration="P1Y",
        planCode="zone",
        pricingMode="default"
    )
    itemId = result.get("idemId")
    # Configure the DNS zone in the cart
    client.post(
        f'/order/cart/{cart_id}/item/{ itemId }/configuration',
        label="zone",
        value=domain
    )

# Finalyze your order
checkout = client.post(
    f'/order/cart/{cart_id}/checkout'
)
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Pierre
  • 2,552
  • 5
  • 26
  • 47
  • Thank you! Do you know if it is possible to add an array (e.g. 20) of zones at one time? – sphtd97 Feb 20 '23 at 09:32
  • It depends: is to 20 totally different zones ? If some, simply loop over an array. Is it 20 subdomain in the same zone ? If so, wait for the zone to be delivered and then setup these 20 domains in this zone – Pierre Feb 20 '23 at 10:48
  • The 20 different separate new zones. – sphtd97 Feb 20 '23 at 12:15
  • I updated my answer. Simply add a `for` loop that loop over each zones you want to order. Add each zone to the same cart as different items. In the end you'll have 1 cart with 20 items – Pierre Feb 20 '23 at 13:42
  • Thank you bro! I just run script and it. I just ran the script and it showed that it wanted quantity, so I added quantity=1 and now it just is ovh.exceptions.APIError: You must login first. But i aded credentials (keys) – sphtd97 Feb 20 '23 at 15:55
  • Your keys might be wrong. I updated my answer to add a link to the doc. To generate the keys use https://www.ovh.com/auth/api/createToken (ensure to add all the needed routes) Also check https://docs.ovh.com/gb/en/api/first-steps-with-ovh-api/#advanced-usage-pair-ovhcloud-apis-with-an-application_2 – Pierre Feb 20 '23 at 16:35
  • I added /order/cart/* (GET and POST) so i dont know what is wrong. I will try generate a new pairs. Did you had that eror ever? – sphtd97 Feb 20 '23 at 17:00
  • Never had this error but it's pretty clear: your 3 keys are not correct, or you are not using the `client` you instanciated – Pierre Feb 21 '23 at 08:31
  • Ok, now it's ok, except for the payment problem. If adding a zone via the GUI is free, why does it require me to add a payment method via the API? I added to f'/order/cart/{cart_id}/checkout', autoPayWithPreferredPaymentMethod=False ( template not found) or autoPayWithPreferredPaymentMethod=True (You do not have preferred payment method) – sphtd97 Feb 21 '23 at 09:15
  • Never used the auto-pay feature with the API, can't help you with this. Double check the API calls made by the GUI. For instance I spotted a `POST /order/cart/{cart_id}/item/{ itemId }/configuration` with a `label: "template", value: "minimized"` in the calls, maybe that's the one you're missing. – Pierre Feb 21 '23 at 09:36