Package: convex
ConvexThe official Python client forConvex.Write and read data from a Convex backend with queries, mutations, and actions.
Get up and running atdocs.convex.dev.Installation:pip install convexBasic usage:>>>fromconveximportConvexClient>>>client=ConvexClient('https://example-lion-123.convex.cloud')>>>messages=client.query("messages:list")>>>frompprintimportpprint>>>pprint(messages)[{'_creationTime':1668107495676.2854,'_id':'2sh2c7pn6nyvkexbdsfj66vd9h5q3hg','author':'Tom','body':'Have you tried Convex?'},{'_creationTime':1668107497732.2295,'_id':'1f053fgh2tt2fc93mw3sn2x09h5bj08','author':'Sarah','body':"Yeah, it's working pretty well for me."}]>>>client.mutation("sendMessage",dict(author="Me",body="Hello!"))To find the url of your convex backend, open the deployment you want to work
with in the appropriate project in theConvex dashboardand click "Settings" where the
Deployment URL should be visible. To find out which queries, mutations, and
actions are available check the Functions pane in the dashboard.To see logs emitted from Convex functions, set the debug mode to True.>>>client.set_debug(True)To provide authentication for function execution, callset_auth().>>>client.set_auth("token-from-authetication-flow")Join us on Discordto get your questions
answered or share what you're doing with Convex. If you're just getting started,
seehttps://docs.convex.devto see how to quickly spin up a backend that does
everything you need in the Convex cloud.Convex typesConvex backend functions are written in JavaScript, so arguments passed to
Convex RPC functions in Python are serialized, sent over the network, and
deserialized into JavaScript objects. To learn about Convex's supported types
seehttps://docs.convex.dev/using/types.In order to call a function that expects a JavaScript type, use the
corresponding Python type or any other type that coerces to it. Values returned
from Convex will be of the corresponding Python type.JavaScript TypePython TypeExampleOther Python Types that ConvertnullNoneNonebigintConvexBigInt (see below)ConvexInt64(2**60)numberfloatorint3.1,10booleanboolTrue,Falsestringstr'abc'ArrayBufferbytesb'abc'ArrayBufferArraylist[1, 3.2, "abc"]tuple, collections.abc.Sequenceobjectdict{a: "abc"}collections.abc.MappingInts and FloatsWhileConvex supports storing Int64s and Float64s,
idiomatic JavaScript pervasively uses the (floating point)Numbertype. In
Pythonfloats are often understood to contain theints: thefloattype
annotation isgenerally understood asUnion[int, float].Therefore, the Python Convex client converts Python'sfloats andints to aFloat64in Convex.To specify a JavaScript BigInt, use the ConvexInt64 class. Functions which
return JavaScript BigInts will return ConvexBigInt64 instances.Convex ErrorsThe Python client supports theConvexErrortype to hold application errors
that are propagated from your Convex functions. To learn about how to throwConvexErrors seehttps://docs.convex.dev/functions/error-handling/application-errors.On the Python client,ConvexErrors are Exceptions with adatafield that
contains someConvexValue. Handling application errors from the Python client
might look something like this:importconvexclient=convex.ConvexClient('https://happy-animal-123.convex.cloud')try:client.mutation("messages:sendMessage",{body:"hi",author:"anjan"})exceptconvex.ConvexErroraserr:ifisinstance(err.data,dict):if"code"inerr.dataanderr.data["code"]==1:# do somethingelse:# do something elseelifisinstance(err.data,str):print(err.data)exceptExceptionaserr:# log internallyPaginationPaginated queriesare queries
that accept pagination options as an argument and can be called repeatedly to
produce additional "pages" of results.For a paginated query like this:import{query}from"./_generated/server";exportdefaultquery(async({db},{paginationOpts})=>{returnawaitdb.query("messages").order("desc").paginate(paginationOpts);});and returning all results 5 at a time in Python looks like this:importconvexclient=convex.ConvexClient('https://happy-animal-123.convex.cloud')done=Falsecursor=Nonedata=[]whilenotdone:result=client.query('listMessages',{"paginationOpts":{"numItems":5,"cursor":cursor}})cursor=result['continueCursor']done=result["isDone"]data.extend(result['page'])print('got',len(result['page']),'results')print('collected',len(data),'results')VersioningWhile we are pre-1.0.0, we'll update the minor version for large changes, and
the patch version for small bugfixes. We may make backwards incompatible changes
to the python client's API, but we will limit those to minor version bumps.