Notes for AI coding agents that write code with this SDK, or work in this repository.
- This is not python-ovh (
pip install ovh,import ovh). Do not mix them. Here:pip install ovhcloud,import ovhcloud. - Credentials are an OAuth2 service account (
OVH_CLIENT_ID,OVH_CLIENT_SECRET,OVH_REGION). No application key, consumer key orovh.conf. - Each API call has a typed method: python-ovh's
client.get("/me")isclient.me.get_account_details(). A body is a typed model, not keyword arguments.
-
Each API section is a client attribute, in snake_case:
client.me,client.public_cloud(thepublicCloudAPI). Its types are in itsmodelsmodule:from ovhcloud.api.public_cloud import models. -
To find the method of a route, read
index/<section>.jsonl. It has one JSON line per method, with the route, the Python signature, the summary, the IAM actions (iam), and the module to import. It is at the root of this repository, and in the installed package (ovhcloud/index/, next to this file andllms.txt). Search it before you guess a name. From a project that uses the SDK, this searches the version it uses:grep -h '"path":"/v1/me"' \ "$(python -c 'import ovhcloud, os; print(os.path.dirname(ovhcloud.__file__))')"/index/*.jsonl
-
To tell the user what to grant the service account, list the
iamof the methods your program calls, from the index or from the docstrings. -
The docstring of each method starts with its route:
get_account_details calls GET /v1/me. It then gives the API's description, the arguments, the IAM actions the call needs, and whether it is deprecated. A deprecated method names its replacement route when the API gives one: look it up in the index too.all_xiterators say which list they walk, its arguments, and the IAM actions of both calls. A deprecated model field saysDeprecatedin its docstring. -
help(ovhcloud.api.<section>)gives the section's size, its route prefixes, the list methods to start from, and its index file. -
Routes start with their API version,
/v1/or/v2/. The same path can exist in both. -
At run time,
ovhcloud.lookup_operation("me.get_account_details")returns an operation's route, summary and IAM actions,ovhcloud.match_operation("GET", "/v1/me/sshKey/my-key")finds the operation of a concrete path, andovhcloud.operations()lists them all.
- One client for the whole program:
client = ovhcloud.Client(). It readsOVH_CLIENT_ID,OVH_CLIENT_SECRETandOVH_REGION(eu,caorus). It is safe to share between threads. Use it in awithblock, or callclient.close(). - In an asyncio program, use
ovhcloud.AsyncClient()instead: the same arguments and methods, withawait. Iterators are async (async for). Wait withawait ovhcloud.async_wait_ready(...)and the otherasync_forms. - Arguments come in this order: path parameters, required query parameters, the request body. Optional parameters are keyword-only, after them. Leave out what you do not set.
- Names are snake_case: methods (
get_ssh_key), arguments (key_name), fields (ip_address). Classes keep the API's acronyms upper case:DedicatedServerIPBlock. Enum members are UPPER_CASE:WidgetStateEnum.OK. Check themodelsmodule before you use one. - A request body is a model of the section:
models.WidgetWidgetWritable(name="x"). Only the fields you set are sent. A field set toNoneis sent asnull. A dict body is turned into the model first: a dict that does not match raisesovhcloud.InvalidRequestError. - A
datetimeyou send needs a time zone:datetime.datetime.now(datetime.UTC). A naive one raisesovhcloud.InvalidRequestError. - A request body has no read-only field. A body whose type has some takes
<Type>Writableinstead (get_env_varreturnsHostingWebEnvVarAlt,update_env_vartakesHostingWebEnvVarWritable). To change an object: get it, callobj.writable(), change the fields you need, send it. A field markedRead-onlyin a returned type is set by the API. - Enums are open. A value the SDK does not know is still accepted, as a new member. A string
works where a method takes an enum, and in an enum field of a
<Type>Writableor of an inline request body:WidgetWidgetWritable(name="web", state="ok"). Elsewhere, pyright wants the member: in a list of enums ([WidgetStateEnum.OK]), and in a body type that is also a response type (noWritableform). At run time a string works everywhere. - To log or return a result, use
obj.to_dict()orobj.to_json(): API names, only the fields the API sent. Notmodel_dump(): it gives snake_case names andNonefor every field that was not sent. - A response that does not match its schema is still returned. Then a required field
(
id: str) can beNone.ovhcloud.is_validated(obj)isFalsefor such an object. list_xreturns a list, often of IDs only.all_xis an iterator over the objects: aforloop gets them one at a time. An error stops the loop with an exception.- Paginated v2 lists have only
all_x. There is no method that loads every page at once: uselist(client.<section>.all_<x>(...))if you need them all. - A path parameter must not be empty: the call fails before it sends anything, with
ovhcloud.InvalidRequestError. - No method for a route yet? Call it directly:
client.get("/v1/me"),client.post("/v1/...", {"key": "value"}). It returns plain dicts and lists, or the type you pass asresult=:client.get("/v1/me/sshKey", result=list[str]). Build a path with parameters withovhcloud.path("/v1/me/sshKey/{keyName}", keyName=name): it escapes them.
-
Many calls only start the work. A v2 resource comes back
CREATINGorUPDATING. A v1 call often returns a task. Do not sleep in a loop: wait with the GET of the same object. -
ovhcloud.wait_ready(get)polls untilresource_statusisREADY.ovhcloud.wait_task(get)polls until a task'sstatusisdone.ovhcloud.wait_gone(get)polls until a 404, after a delete.getis a function with no argument that calls the generated GET:instance = ovhcloud.wait_ready(lambda: client.public_cloud.get_instance(project_id, instance_id))
-
A failed status (
ERROR, a task in error or waiting for an action) raisesovhcloud.WaitFailedError. The wait gives up after 30 minutes withovhcloud.WaitTimeoutError:timeout=changes that.ovhcloud.wait(get, done)takes any condition.
- Every exception of the SDK is an
ovhcloud.OvhError. An error status from the API is anovhcloud.ApiError. Its message is the API's own. - Catch a common status by its class:
NotFoundError,ForbiddenError,UnauthorizedError,BadRequestError,ConflictError,TooManyRequestsError. - Read
status_code,error_class,message,query_id(for support) andoperation, the method's name, on anyApiError. - A 403 means the service account lacks an IAM action. The message ends with the actions the
call needs (
requires the IAM action account:apiovh:me/get), also iniam_actions: report them to the user, who can grant them. Retrying does not help. - A request the client is configured to refuse raises
ovhcloud.NotAllowedError, and nothing is sent. Do not work around it: ask the user. - With
dry_run=True, a call that would change something raisesovhcloud.DryRunError. It is not a failure: it tells what would have been sent. - A timeout raises
ovhcloud.RequestTimeoutError. It is aTransportErrorand aTimeoutError. It is never retried: the server may have done the work.
- To explore an account safely, use a read-only client:
ovhcloud.Client(read_only=True)sends onlyGETandHEAD.read_only=["cloud.reboot_project_instance"]also allows that one operation.allowed_operations=["me.*", "*.get_*"]sends only the operations whose name matches. A request must pass both when both are set. - To plan a change before making it, use
ovhcloud.Client(dry_run=True). Reads are sent, so you see the real state. Any other call is not sent: it raisesovhcloud.DryRunErrorwith themethod, the fullurl, theheadersand the JSONbodyit would have sent. Show these to the user, then make the change with a normal client once they agree. - To log or veto calls, use
before_request=hook(raise an exception to stop the call: the caller getsovhcloud.HookRefusedError) andafter_request=hook(sees every call, with itsstatus_code,query_id,error,durationandattempts). The hooks get aCallInfowith the operation, socall.operation.nameandcall.operation.read_onlytell what the call does. Each page of anall_xiterator is a separate call. Calls made at the same time run a hook at the same time, so a hook must be safe for that. - There is no timeout by default. Set one:
ovhcloud.Client(timeout=30). - Retries are off by default.
retry=Trueretries onlyGET,HEADandDELETE, neverPOST,PUTorPATCH: those can start tasks or place orders. In tests, passretry_sleep=lambda s: Noneso a mocked503does not really wait. debug=Truelogs requests on theovhcloudlogger, with credentials hidden.debug_bodies=Truealso logs bodies, which can hold secrets.- For tracing, metrics or the
X-Ovh-Queryidof a call that succeeds, pass your ownhttp_client=httpx.Client(event_hooks=...), or use the OpenTelemetry httpx instrumentation. Settimeout=then: httpx's own default is 5 seconds.
- No network and no credentials:
ovhcloud.Client(token_source=ovhcloud.StaticTokenSource("t")), and mock the API with respx (respx.mock(base_url="https://api.eu.ovhcloud.com")) orhttp_client=httpx.Client(transport=httpx.MockTransport(handler)). Mock paths with their version:/v1/me. The host is the region's:api.ca.ovhcloud.comforregion="ca". - With
retry=, passretry_sleep=lambda s: None(anasyncfunction forAsyncClient). - Answer with JSON, and an error status for an error: a 404 makes the call raise
ovhcloud.NotFoundError. - A fake return value is a model built with its required fields and the fields your code
reads:
models.WidgetWidget(id="w-1", name="web"). - One
AsyncClientper event loop: create it in the test or a fixture of the same loop scope, never at module level.
src/ovhcloud/api/,index/andllms.txtare generated at each release. Do not edit them: the next release overwrites the change. Report the problem instead, with the route.src/ovhcloud/_core/is hand-written. Each export replaces this whole repository, so a change made here is ported by the maintainers into the generator: see CONTRIBUTING.md.- Check a change with
uvx ruff check srcanduvx pyright --pythonpath .venv/bin/python src/ovhcloud/_core. The SDK's tests are not part of this repository. - examples/ has small programs that call real methods. It is in the repository and the sdist, not in the installed package.
More: README.md is the full guide.