How to create course using API?

I have redered /api-docs but seems there is no api for creating course. How can I create course using API via postman?

Hi @bhumi,

There is a simple technique you can use to answer such questions:

  1. Go to the Studio home page and click “New Course”
  2. Right-click anywhere on the page and click “Inspect” or the equivalent in your browser
  3. Go to the “Network” tab. Clear any entries currently there and enable “Preserve Log”
  4. Fill out the New Course form and hit Create.
  5. Check the Network log to see the request:
POST /course/
{org: "OpenCraftX", number: "TEST", display_name: "A Test Course", run: "101"}

^ That is the API you can use to create a course.

1 Like

@braden Api is giving me “CSRF verification failed. Request aborted.” error while calling from postman

I have passed X-CSRFToken:{{csrftoken}} in header and this line in tests section of postman

var xsrfCookie = postman.getResponseCookie(“csrftoken”); postman.setEnvironmentVariable(‘csrftoken’, xsrfCookie.value);

still getting same error

Did you generate the CSRF token using the same credentials (e.g. OAuth bearer token or JWT) that you’re using to authenticate the request? Try using the /csrf/api/v1/token API endpoint to retrieve the CSRF token, then submit it with X-CSRFToken.

I have taken csrf toekn from browser cookie. I have called csrf/api/v1/token and it’s returning token but still getting 403 error

I have called

/csrf/api/v1/token without any payload and it’s returning

{
“csrfToken”: “”
}

I am passing this token in /couse api header like

X-CSRFToken:

but getting same error

@braden I have tried calling api using csrftoken from cookie and /course api is giving 200 status code but it’s not creating course inside the openedx

Are you checking for the course using the same user that created the course? What happens if you try creating the exact same course twice?

Basically, I suspect that it’s working but you’re not seeing the course due to permissions or something.

I am getting 200 status only so not getting if it is creating course or not. How can I fix the issue? Which permission I need to set? Also, response is html content. It’s not giving created course id

Are you setting the Accept: application/json header? Because that is required. This endpoint is shared by both HTML and JSON responses. See the code here for details.

Yes, I have set application/json in content-type header